1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
package com.yahoo.documentapi.local;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
import com.yahoo.document.DocumentPut;
import com.yahoo.documentapi.AckToken;
import com.yahoo.documentapi.ProgressToken;
import com.yahoo.documentapi.VisitorControlHandler;
import com.yahoo.documentapi.VisitorDataHandler;
import com.yahoo.documentapi.VisitorDataQueue;
import com.yahoo.documentapi.VisitorParameters;
import com.yahoo.documentapi.VisitorResponse;
import com.yahoo.documentapi.VisitorSession;
import com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage;
import com.yahoo.messagebus.Trace;
import com.yahoo.yolean.Exceptions;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicReference;
/**
* Local visitor session that copies and iterates through all items in the local document access.
* Each document must be ack'ed for the session to be done visiting.
* Only document puts are sent by this session, and this is done from a separate thread.
*
* @author jonmv
*/
public class LocalVisitorSession implements VisitorSession {
private enum State { RUNNING, FAILURE, ABORTED, SUCCESS }
private final VisitorDataHandler data;
private final VisitorControlHandler control;
private final Map<DocumentId, Document> outstanding;
private final AtomicReference<State> state;
public LocalVisitorSession(LocalDocumentAccess access, VisitorParameters parameters) {
if (parameters.getResumeToken() != null)
throw new UnsupportedOperationException("Continuation via progress tokens is not supported");
if (parameters.getRemoteDataHandler() != null)
throw new UnsupportedOperationException("Remote data handlers are not supported");
this.data = parameters.getLocalDataHandler() == null ? new VisitorDataQueue() : parameters.getLocalDataHandler();
this.data.reset();
this.data.setSession(this);
this.control = parameters.getControlHandler() == null ? new VisitorControlHandler() : parameters.getControlHandler();
this.control.reset();
this.control.setSession(this);
this.outstanding = new ConcurrentSkipListMap<>(access.documents);
this.state = new AtomicReference<>(State.RUNNING);
start();
}
void start() {
new Thread(() -> {
try {
// Iterate through all documents and pass on to data handler
outstanding.forEach((id, document) -> {
data.onMessage(new PutDocumentMessage(new DocumentPut(document)),
new AckToken(id));
});
// Transition to a terminal state when done
state.updateAndGet(current -> {
switch (current) {
case RUNNING:
control.onDone(VisitorControlHandler.CompletionCode.SUCCESS, "Success");
return State.SUCCESS;
case ABORTED:
control.onDone(VisitorControlHandler.CompletionCode.ABORTED, "Aborted by user");
return State.ABORTED;
default:
control.onDone(VisitorControlHandler.CompletionCode.FAILURE, "Unexpected state '" + current + "'");;
return State.FAILURE;
}
});
}
// Transition to failure terminal state on error
catch (Exception e) {
state.set(State.FAILURE);
outstanding.clear();
control.onDone(VisitorControlHandler.CompletionCode.FAILURE, Exceptions.toMessageString(e));
}
finally {
data.onDone();
}
}).start();
}
@Override
public boolean isDone() {
return outstanding.isEmpty();
}
@Override
public ProgressToken getProgress() {
throw new UnsupportedOperationException("Progress tokens are not supported");
}
@Override
public Trace getTrace() {
throw new UnsupportedOperationException("Traces are not supported");
}
@Override
public boolean waitUntilDone(long timeoutMs) throws InterruptedException {
return control.waitUntilDone(timeoutMs);
}
@Override
public void ack(AckToken token) {
outstanding.remove((DocumentId) token.ackObject);
}
@Override
public void abort() {
state.updateAndGet(current -> current == State.RUNNING ? State.ABORTED : current);
outstanding.clear();
}
@Override
public VisitorResponse getNext() {
return data.getNext();
}
@Override
public VisitorResponse getNext(int timeoutMilliseconds) throws InterruptedException {
return data.getNext(timeoutMilliseconds);
}
@Override
public void destroy() {
if ( ! isDone())
abort();
}
}
|