diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/HBasePlugin/HBasePlugin.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/HBasePlugin/HBasePlugin.java new file mode 100644 index 00000000..79b67257 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/HBasePlugin/HBasePlugin.java @@ -0,0 +1,77 @@ +package org.bench4q.agent.plugin.basic.HBasePlugin; + +import java.util.UUID; + +import org.bench4q.agent.plugin.Constructor; +import org.bench4q.agent.plugin.Parameter; +import org.bench4q.agent.plugin.Plugin; +import org.bench4q.agent.plugin.behavior.Behavior; +import org.bench4q.agent.plugin.behavior.BehaviorType; +import org.bench4q.agent.utils.Type.SupportTypes; +import org.bench4q.share.exception.Bench4QRunTimeException; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.client.HBaseAdmin; +import org.apache.hadoop.hbase.client.HTable; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Result; +import org.apache.hadoop.hbase.client.ResultScanner; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; +import org.apache.hadoop.hbase.filter.Filter; +import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.log4j.Logger; + +@Plugin("HBase") +public class HBasePlugin { + private final Configuration conf; + private final HTable tableUnderTest; + + @Constructor + public HBasePlugin() { + conf = HBaseConfiguration.create(); + try { + @SuppressWarnings({ "unused", "resource" }) + HBaseAdmin admin = new HBaseAdmin(conf); + tableUnderTest = new HTable(conf, "users"); + } catch (Exception e) { + e.printStackTrace(); + throw new Bench4QRunTimeException("Construct HBasePlugin fails!", e); + } + } + + @Behavior(value = "Insert", type = BehaviorType.USER_BEHAVIOR) + public HBaseReturn insert( + @Parameter(value = "key", type = SupportTypes.Field) String key, + @Parameter(value = "value", type = SupportTypes.Field) String value) { + try { + Put put = new Put(UUID.randomUUID().toString().getBytes()); + put.add("key".getBytes(), "key".getBytes(), key.getBytes()); + put.add("value".getBytes(), "value".getBytes(), value.getBytes()); + this.tableUnderTest.put(put); + } catch (Exception ex) { + Logger.getLogger(HBasePlugin.class).info(ex, ex); + return new HBaseReturn(false); + } + return new HBaseReturn(true); + } + + public HBaseReturn Query( + @Parameter(value = "key", type = SupportTypes.Field) String key) { + try { + Filter filter = new SingleColumnValueFilter(Bytes.toBytes("key"), + Bytes.toBytes("key"), CompareOp.EQUAL, Bytes.toBytes("aaa")); // 当列column1的值为aaa时进行查询 + Scan s = new Scan(); + s.setFilter(filter); + ResultScanner resultScanner = this.tableUnderTest.getScanner(s); + for (Result r : resultScanner) { + System.out.println("get the row key:" + new String(r.getRow())); + } + } catch (Exception ex) { + Logger.getLogger(HBasePlugin.class).info(ex, ex); + return new HBaseReturn(false); + } + return new HBaseReturn(true); + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/HBasePlugin/HBaseReturn.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/HBasePlugin/HBaseReturn.java new file mode 100644 index 00000000..bef2ddc5 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/HBasePlugin/HBaseReturn.java @@ -0,0 +1,9 @@ +package org.bench4q.agent.plugin.basic.HBasePlugin; + +import org.bench4q.agent.plugin.basic.PluginReturn; + +public class HBaseReturn extends PluginReturn { + public HBaseReturn(boolean success) { + super(success); + } +} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/LRUCache.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/LRUCache.java new file mode 100644 index 00000000..077acb64 --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/LRUCache.java @@ -0,0 +1,113 @@ +package org.bench4q.agent.test; + +import static org.junit.Assert.*; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +import org.junit.Test; + +public class LRUCache { + private int capacity; + private ListNode vHead; + private int last; + private Map keyNodes; + + public LRUCache(int capacity) { + vHead = new ListNode(-1, -1, null, null); + this.keyNodes = new HashMap(capacity); + this.capacity = capacity; + } + + public int get(int key) { + if (this.keyNodes.containsKey(key)) { + ListNode node = this.keyNodes.get(key); + delete(node); + offerFirst(node); + return node.val; + } + return -1; + } + + private void delete(ListNode node) { + if (node.key == this.last) { + if (this.keyNodes.size() <= 1) { + this.last = Integer.MAX_VALUE; + } else { + this.last = node.previous.key; + } + } + node.previous.next = node.next; + if (node.next != null) { + node.next.previous = node.previous; + } + node.previous = null; + node.next = null; + this.keyNodes.remove(node.key); + } + + private void offerFirst(ListNode node) { + node.next = vHead.next; + if (vHead.next != null) { + vHead.next.previous = node; + } + node.previous = vHead; + vHead.next = node; + if (this.keyNodes.size() == 0) { + this.last = node.key; + } + this.keyNodes.put(node.key, node); + } + + public void set(int key, int value) { + ListNode valNode = null; + if (this.keyNodes.containsKey(key)) { + valNode = this.keyNodes.get(key); + valNode.val = value; + delete(valNode); + } else { + if (isFull()) { + delete(this.keyNodes.get(this.last)); + } + valNode = new ListNode(key, value, null, null); + } + offerFirst(valNode); + } + + private boolean isFull() { + return this.keyNodes.size() == this.capacity; + } + + private class ListNode { + int key; + int val; + ListNode previous; + ListNode next; + + public ListNode(int key, int val, ListNode previous, ListNode next) { + this.key = key; + this.val = val; + this.previous = previous; + this.next = next; + } + } + + public static void main(String[] args) { + LRUCache cache = new LRUCache(3); + cache.set(1, 1); + cache.set(2, 2); + cache.set(3, 3); + cache.set(4, 4); + System.out.println(cache.get(4)); + System.out.println(cache.get(3)); + System.out.println(cache.get(2)); + System.out.println(cache.get(1)); + cache.set(5, 5); + System.out.println(cache.get(1)); + System.out.println(cache.get(2)); + System.out.println(cache.get(3)); + System.out.println(cache.get(4)); + System.out.println(cache.get(5)); + } +} \ No newline at end of file diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/Solution.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/Solution.java new file mode 100644 index 00000000..f96b65d1 --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/Solution.java @@ -0,0 +1,110 @@ +package org.bench4q.agent.test; + +import org.junit.Test; + +class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + next = null; + } +} + +public class Solution { + private static boolean FIRST = true; + private static boolean SECOND = false; + + public void reorderList(ListNode head) { + if (head == null || head.next == null || head.next.next == null) { + return; + } + ListNode startOfSecondHalf = findStartNodeOfSecondHalf(head); + startOfSecondHalf = reverse(startOfSecondHalf); + merge(head, startOfSecondHalf); + } + + private ListNode findStartNodeOfSecondHalf(ListNode head) { + ListNode vHead = new ListNode(-1); + vHead.next = head; + ListNode runner = head, walker = head, walkerPre = vHead; + while (runner != null && runner.next != null) { + runner = runner.next; + runner = runner.next; + walkerPre = walkerPre.next; + walker = walker.next; + } + walkerPre.next = null; + return walker; + } + + private ListNode reverse(ListNode start) { + ListNode vHead = new ListNode(-1), current = start, next = start.next; + while (current != null) { + current.next = vHead.next; + vHead.next = current; + current = next; + if (next == null) { + break; + } + next = next.next; + } + return vHead.next; + } + + private void merge(ListNode head, ListNode head2) { + ListNode current = head, firstCurrent = head.next, secondCurrent = head2, firstNext = firstCurrent != null ? firstCurrent.next + : null, secondNext = head2.next; + boolean currentTurn = SECOND; + while (firstCurrent != null && secondCurrent != null) { + if (currentTurn == FIRST) { + firstCurrent.next = null; + current.next = firstCurrent; + firstCurrent = firstNext; + if (firstNext != null) { + firstNext = firstNext.next; + } + currentTurn = SECOND; + } else { + secondCurrent.next = null; + current.next = secondCurrent; + secondCurrent = secondNext; + if (secondNext != null) { + secondNext = secondNext.next; + } + currentTurn = FIRST; + } + current = current.next; + } + + current.next = firstCurrent == null ? secondCurrent : firstCurrent; + } + + @Test + public void test() { + ListNode list = new ListNode(1); + list.next = new ListNode(2); + list.next.next = new ListNode(3); + list.next.next.next = new ListNode(4); + list.next.next.next.next = new ListNode(5); + reorderList(list); + print(list); + } + + private void print(ListNode list) { + while (list != null) { + System.out.println(list.val); + list = list.next; + } + } + + @Test + public void test1() { + ListNode list = new ListNode(1); + list.next = new ListNode(2); + list.next.next = new ListNode(3); + reorderList(list); + print(list); + } +} \ No newline at end of file diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Agent.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Agent.java index 179075c5..fe2706e6 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Agent.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Agent.java @@ -80,6 +80,17 @@ public class Agent { this.currentEnumStatus = currentEnumStatus; } + public static Agent createAgentWithoutId(Agent agentWithoutInitial) { + if (agentWithoutInitial.getMaxLoad() > 0) { + return createAgentWithoutId(agentWithoutInitial.getHostName(), + agentWithoutInitial.getPort(), + agentWithoutInitial.getMaxLoad()); + } else { + return createAgentWithoutId(agentWithoutInitial.getHostName(), + agentWithoutInitial.getPort()); + } + } + public static Agent createAgentWithoutId(String hostName, int port) { Agent agent = new Agent(); agent.setCurrentEnumStatus(AgentStatus.InIdle); diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java index 1aaf109d..9b4821c7 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java @@ -51,8 +51,7 @@ public class AgentService { public boolean addAgentToPool(Agent agentWithoutId) { synchronized (this.getAgentRepository().getAddDeleteLock()) { if (!this.getAgentRepository().attach( - Agent.createAgentWithoutId(agentWithoutId.getHostName(), - agentWithoutId.getPort()))) { + Agent.createAgentWithoutId(agentWithoutId))) { return false; } Agent agent = this.getAgentRepository().getAgentBy( diff --git a/Bench4Q-Master/src/main/resources/org/bench4q/master/config/hibernate.cfg.xml b/Bench4Q-Master/src/main/resources/org/bench4q/master/config/hibernate.cfg.xml index 8541a436..062ad7d4 100644 --- a/Bench4Q-Master/src/main/resources/org/bench4q/master/config/hibernate.cfg.xml +++ b/Bench4Q-Master/src/main/resources/org/bench4q/master/config/hibernate.cfg.xml @@ -7,7 +7,7 @@ root 123456 - 20 + 100 false true true