Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be7b595

Browse files
testing batch size , timeout
1 parent 7da8294 commit be7b595

File tree

5 files changed

+241
-0
lines changed

5 files changed

+241
-0
lines changed

‎javademo/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@
115115
<version>0.7.2</version>
116116
</dependency>
117117

118+
<!-- picocli -->
119+
<dependency>
120+
<groupId>info.picocli</groupId>
121+
<artifactId>picocli</artifactId>
122+
<version>4.0.1</version>
123+
</dependency>
124+
118125
<!-- metrics -->
119126
<dependency>
120127
<groupId>io.dropwizard.metrics</groupId>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package timer;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Map.Entry;
7+
import java.util.Timer;
8+
import java.util.TimerTask;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
import java.util.concurrent.TimeUnit;
11+
import util.SimpleLogger;
12+
13+
/**
14+
*
15+
*/
16+
public class BatchTimer {
17+
18+
private long blockNumber;
19+
private int activeNodeCount;
20+
private int remains;
21+
22+
Timer timer;
23+
TimerTask timerTask;
24+
long timeoutMillis;
25+
26+
private ConcurrentHashMap<String, List<String>> nodes = new ConcurrentHashMap<>();
27+
28+
public BatchTimer(long blockNumber, int activeNodeCount, long timeout, TimeUnit timeUnit) {
29+
this.blockNumber = blockNumber;
30+
this.activeNodeCount = activeNodeCount;
31+
this.remains = activeNodeCount;
32+
33+
timer = new Timer();
34+
timerTask = new TimerTask() {
35+
@Override
36+
public void run() {
37+
System.out.println(">> Cutting block becuz of timeout");
38+
cuttingBlocks();
39+
}
40+
};
41+
timeoutMillis = timeUnit.toMillis(timeout);
42+
}
43+
44+
public void newBlock(String blockHash, String node) {
45+
// first come
46+
if (nodes.isEmpty()) {
47+
timer.schedule(timerTask, timeoutMillis, timeoutMillis);
48+
}
49+
50+
List<String> nodesByHash = nodes.get(blockHash);
51+
52+
if (nodesByHash == null) {
53+
nodesByHash = new ArrayList<>(activeNodeCount);
54+
nodes.put(blockHash, nodesByHash);
55+
}
56+
57+
nodesByHash.add(node);
58+
59+
remains--;
60+
61+
if (remains == 0) {
62+
cuttingBlocks();
63+
}
64+
}
65+
66+
private void cuttingBlocks() {
67+
SimpleDateFormat sdf = new SimpleDateFormat("HH-mm-dd");
68+
long time = System.currentTimeMillis();
69+
SimpleLogger.info("## cutting blocks..");
70+
71+
if (timer != null) {
72+
timer.cancel();
73+
}
74+
75+
if (nodes.isEmpty()) {
76+
SimpleLogger.print("## Received empty block..");
77+
return;
78+
}
79+
80+
if (nodes.size() == 1) {
81+
Entry<String, List<String>> entry = nodes.entrySet().iterator().next();
82+
SimpleLogger.println("## Received same hash {} from {} nodes."
83+
, entry.getKey(), entry.getValue().size());
84+
85+
return;
86+
}
87+
88+
SimpleLogger.println("## Received diff hash {}", nodes.size());
89+
for (Entry<String, List<String>> entry : nodes.entrySet()) {
90+
91+
StringBuilder nodesString = new StringBuilder();
92+
for (String nodeName : entry.getValue()) {
93+
nodesString.append(nodeName)
94+
.append(" ");
95+
}
96+
97+
SimpleLogger.println("> Hash : {} ==> {}", entry.getKey(), nodesString.toString());
98+
99+
}
100+
}
101+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cli.pico;
2+
3+
import java.io.File;
4+
import picocli.CommandLine;
5+
import picocli.CommandLine.Option;
6+
import picocli.CommandLine.Parameters;
7+
8+
/**
9+
*
10+
*/
11+
public class Temp {
12+
13+
public static void main(String[] args) {
14+
Tar tar = new Tar();
15+
tar.create = true;
16+
17+
CommandLine commandLine = new CommandLine(tar);
18+
System.out.println(commandLine.toString());
19+
}
20+
21+
public static class Tar {
22+
23+
@Option(names = "-c", description = "create a new archive")
24+
boolean create;
25+
26+
@Option(names = {"-f", "--file"}, paramLabel = "ARCHIVE", description = "the archive file")
27+
File archive;
28+
29+
@Parameters(paramLabel = "FILE", description = "one ore more files to archive")
30+
File[] files;
31+
32+
@Option(names = {"-h", "--help"}, usageHelp = true, description = "display a help message")
33+
private boolean helpRequested = false;
34+
}
35+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package timer;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.concurrent.TimeUnit;
5+
import org.junit.Test;
6+
import util.SimpleLogger;
7+
8+
/**
9+
*
10+
*/
11+
public class BatchTimerTets {
12+
13+
@Test
14+
public void test() throws Exception {
15+
SimpleLogger.info("## Start tests..");
16+
BatchTimer batchTimer = new BatchTimer(1L, 5, 10000, TimeUnit.MILLISECONDS);
17+
18+
StubProducer p1 = new StubProducer(batchTimer, 5000L, "AA", "node1");
19+
StubProducer p2 = new StubProducer(batchTimer, 1000L, "AA", "node2");
20+
StubProducer p3 = new StubProducer(batchTimer, 2200L, "AA", "node3");
21+
StubProducer p4 = new StubProducer(batchTimer, 3300L, "BB", "node4");
22+
StubProducer p5 = new StubProducer(batchTimer, 20000L, "BB", "node5");
23+
24+
StubProducer[] ps = new StubProducer[]{
25+
p1, p2, p3, p4, p5
26+
};
27+
28+
for (StubProducer p : ps) {
29+
Thread t = new Thread(p);
30+
t.setDaemon(true);
31+
t.start();
32+
}
33+
34+
SimpleLogger.info("## End tests..");
35+
TimeUnit.SECONDS.sleep(30);
36+
}
37+
38+
public static class StubProducer implements Runnable {
39+
40+
private BatchTimer batchTimer;
41+
private long initDelay;
42+
private String hash;
43+
private String nodeName;
44+
45+
public StubProducer(BatchTimer batchTimer, long initDelay, String hash, String nodeName) {
46+
this.batchTimer = batchTimer;
47+
this.initDelay = initDelay;
48+
this.hash = hash;
49+
this.nodeName = nodeName;
50+
}
51+
52+
@Override
53+
public void run() {
54+
try {
55+
Thread.sleep(initDelay);
56+
} catch (Exception e) {
57+
58+
}
59+
60+
batchTimer.newBlock(hash, nodeName);
61+
}
62+
}
63+
64+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package timer;
2+
3+
import java.util.Timer;
4+
import java.util.TimerTask;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
import org.checkerframework.checker.units.qual.A;
8+
import org.junit.Test;
9+
10+
/**
11+
*
12+
*/
13+
public class TimerDefaultTest {
14+
15+
@Test
16+
public void defaultUseage() throws Exception {
17+
AtomicInteger integer = new AtomicInteger(1);
18+
Timer timer = new Timer();
19+
TimerTask timerTask = new TimerTask() {
20+
@Override
21+
public void run() {
22+
System.out.println("## task is called..");
23+
if (integer.getAndIncrement() != 1) {
24+
System.out.println("# cancel task..");
25+
timer.cancel();
26+
}
27+
}
28+
};
29+
30+
timer.schedule(timerTask, 5000L, 5000L);
31+
TimeUnit.MINUTES.sleep(1L);
32+
}
33+
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /