Skip to content

Commit a8204c0

Browse files
authored
Merge pull request #39 from oneteme/develop
edit
2 parents 3142f93 + 27f977c commit a8204c0

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>io.github.oneteme.traceapi</groupId>
55
<artifactId>traceapi-core</artifactId>
6-
<version>0.0.20</version>
6+
<version>0.0.21</version>
77
<packaging>jar</packaging>
88
<name>traceapi-core</name>
99
<description>traceapi-core</description>

src/main/java/org/usf/traceapi/core/ScheduledSessionDispatcher.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import static org.usf.traceapi.core.State.DISPACH;
1313

1414
import java.util.ArrayList;
15+
import java.util.Collection;
1516
import java.util.List;
1617
import java.util.concurrent.ScheduledExecutorService;
18+
import java.util.function.Consumer;
1719
import java.util.function.Function;
1820
import java.util.function.Predicate;
1921

@@ -50,12 +52,12 @@ public ScheduledSessionDispatcher(SessionDispatcherProperties properties, Predic
5052

5153
public boolean add(Session... sessions) {
5254
if(state != DISABLE) { // CACHE | DISPATCH
53-
safeQueue(q-> addAll(q, sessions));
55+
doSync(q-> addAll(q, sessions));
5456
log.trace("{} sessions buffered", queue.size());
5557
return true;
5658
}
5759
else {
58-
log.warn("{} sessions rejected because dispatcher.state={}", sessions.length, state);
60+
log.warn("{} sessions rejected, dispatcher.state={}", sessions.length, state);
5961
return false;
6062
}
6163
}
@@ -84,60 +86,67 @@ private void dispatch() {
8486
}
8587
catch (Exception e) {// do not throw exception : retry later
8688
log.warn("error while dispatching {} sessions, attempts={} because : {}", cs.size(), attempts, e.getMessage()); //do not log exception stack trace
87-
safeQueue(q-> {
88-
q.addAll(0, cs);
89+
}
90+
if(attempts > 0) { //exception | !dispatch
91+
doSync(q-> {
92+
q.addAll(0, cs);
8993
if(properties.getBufferMaxSize() > -1 && q.size() > properties.getBufferMaxSize()) {
9094
var diff = q.size() - properties.getBufferMaxSize();
9195
q.subList(properties.getBufferMaxSize(), cs.size()).clear(); //remove exceeding cache sessions (LIFO)
9296
log.warn("{} last sessions have been removed from buffer", diff);
9397
}
94-
return null;
9598
});
96-
}
99+
}
97100
}
98101
}
99102

100103
public List<Session> peekSessions() {
101-
return safeQueue(q-> {
104+
return applySync(q-> {
102105
if(q.isEmpty()) {
103106
return emptyList();
104107
}
105-
var s = queue.stream();
108+
var s = q.stream();
106109
if(nonNull(filter)) {
107110
s = s.filter(filter);
108111
}
109112
return s.collect(toCollection(SessionList::new));
110113
});
111114
}
112115

113-
public List<Session> popSessions() {
114-
return safeQueue(q-> {
116+
List<Session> popSessions() {
117+
return applySync(q-> {
115118
if(q.isEmpty()) {
116119
return emptyList();
117120
}
118121
if(isNull(filter)) {
119-
var c = new ArrayList<>(q);
122+
var c = new SessionList(q);
120123
q.clear();
121124
return c;
122125
}
123126
var c = new SessionList(q.size());
124127
for(var it=q.iterator(); it.hasNext();) {
125-
var s = it.next();
126-
if(filter.test(s)) {
127-
c.add(s);
128+
var o = it.next();
129+
if(filter.test(o)) {
130+
c.add(o);
128131
it.remove();
129132
}
130133
}
131134
return c;
132135
});
133136
}
134137

135-
private <T> T safeQueue(Function<List<Session>, T> queueFn) {
138+
private void doSync(Consumer<List<Session>> cons) {
139+
synchronized(queue){
140+
cons.accept(queue);
141+
}
142+
}
143+
144+
private <T> T applySync(Function<List<Session>, T> fn) {
136145
synchronized(queue){
137-
return queueFn.apply(queue);
146+
return fn.apply(queue);
138147
}
139148
}
140-
149+
141150
public void shutdown() throws InterruptedException {
142151
updateState(DISABLE); //stop add Sessions
143152
log.info("shutting down scheduler service");
@@ -146,7 +155,7 @@ public void shutdown() throws InterruptedException {
146155
while(!executor.awaitTermination(5, SECONDS)); //wait for last save complete
147156
}
148157
finally {
149-
dispatch();
158+
tryDispatch();
150159
}
151160
}
152161

@@ -161,6 +170,10 @@ public SessionList() {
161170
public SessionList(int initialCapacity) {
162171
super(initialCapacity);
163172
}
173+
174+
public SessionList(Collection<? extends Session> c) {
175+
super(c);
176+
}
164177
}
165178

166179
@FunctionalInterface

src/main/java/org/usf/traceapi/core/Session.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ default void lock(){
4242
}
4343

4444
default void unlock() {
45-
if(getLock().get() > 0) {
46-
getLock().decrementAndGet();
47-
}
48-
else {
49-
log.warn("no more lock");
50-
}
45+
getLock().decrementAndGet();
5146
}
5247

5348
default boolean wasCompleted() {
54-
return getLock().get() == 0;
49+
var c = getLock().get();
50+
if(c < 0) {
51+
log.warn("illegal session lock state={}, {}", c, this);
52+
return true;
53+
}
54+
return c == 0;
5555
}
5656

5757
static String nextId() {

0 commit comments

Comments
 (0)