-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AbstractStep.java
254 lines (225 loc) · 7.51 KB
/
AbstractStep.java
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.unsafe.impl.batchimport.staging;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.LongPredicate;
import org.neo4j.helpers.Exceptions;
import org.neo4j.kernel.impl.util.MovingAverage;
import org.neo4j.unsafe.impl.batchimport.Configuration;
import org.neo4j.unsafe.impl.batchimport.stats.ProcessingStats;
import org.neo4j.unsafe.impl.batchimport.stats.StatsProvider;
import org.neo4j.unsafe.impl.batchimport.stats.StepStats;
import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.util.Arrays.asList;
/**
* Basic implementation of a {@link Step}. Does the most plumbing job of building a step implementation.
*/
public abstract class AbstractStep<T> implements Step<T>
{
private final StageControl control;
private volatile String name;
@SuppressWarnings( "rawtypes" )
protected volatile Step downstream;
private volatile boolean endOfUpstream;
protected volatile Throwable panic;
private volatile boolean completed;
protected int orderingGuarantees;
protected final LongPredicate rightDoneTicket = new LongPredicate()
{
@Override
public boolean test( long ticket )
{
return doneBatches.get() == ticket;
}
};
// Milliseconds awaiting downstream to process batches so that its queue size goes beyond the configured threshold
// If this is big then it means that this step is faster than downstream.
protected final AtomicLong downstreamIdleTime = new AtomicLong();
// Milliseconds awaiting upstream to hand over batches to this step.
// If this is big then it means that this step is faster than upstream.
protected final AtomicLong upstreamIdleTime = new AtomicLong();
// Number of batches received, but not yet processed.
protected final AtomicInteger queuedBatches = new AtomicInteger();
// Number of batches fully processed
protected final AtomicLong doneBatches = new AtomicLong();
// Milliseconds spent processing all received batches.
protected final MovingAverage totalProcessingTime;
protected long startTime, endTime;
private final List<StatsProvider> additionalStatsProvider;
protected final Runnable healthChecker = () -> assertHealthy();
public AbstractStep( StageControl control, String name, Configuration config,
StatsProvider... additionalStatsProvider )
{
this.control = control;
this.name = name;
this.totalProcessingTime = new MovingAverage( config.movingAverageSize() );
this.additionalStatsProvider = asList( additionalStatsProvider );
}
@Override
public void start( int orderingGuarantees )
{
this.orderingGuarantees = orderingGuarantees;
resetStats();
}
protected boolean guarantees( int orderingGuaranteeFlag )
{
return (orderingGuarantees & orderingGuaranteeFlag) != 0;
}
@Override
public String name()
{
return name;
}
@Override
public void receivePanic( Throwable cause )
{
this.panic = cause;
this.completed = true;
}
protected boolean stillWorking()
{
if ( isPanic() )
{ // There has been a panic, so we'll just stop working
return false;
}
if ( endOfUpstream && queuedBatches.get() == 0 )
{ // Upstream has run out and we've processed everything upstream sent us
return false;
}
// We're still working
return true;
}
protected boolean isPanic()
{
return panic != null;
}
@Override
public boolean isCompleted()
{
return completed;
}
protected void issuePanic( Throwable cause )
{
issuePanic( cause, true );
}
protected void issuePanic( Throwable cause, boolean rethrow )
{
control.panic( cause );
if ( rethrow )
{
throw Exceptions.launderedException( cause );
}
}
protected void assertHealthy()
{
if ( isPanic() )
{
throw new RuntimeException( "Panic called, so exiting", panic );
}
}
@Override
public void setDownstream( Step<?> downstream )
{
assert downstream != this;
this.downstream = downstream;
}
@Override
public StepStats stats()
{
Collection<StatsProvider> providers = new ArrayList<>();
collectStatsProviders( providers );
return new StepStats( name, stillWorking(), providers );
}
protected void collectStatsProviders( Collection<StatsProvider> into )
{
into.add( new ProcessingStats( doneBatches.get() + queuedBatches.get(), doneBatches.get(),
totalProcessingTime.total(), totalProcessingTime.average() / processors( 0 ),
upstreamIdleTime.get(), downstreamIdleTime.get() ) );
into.addAll( additionalStatsProvider );
}
@Override
public void endOfUpstream()
{
endOfUpstream = true;
checkNotifyEndDownstream();
}
protected void checkNotifyEndDownstream()
{
if ( !stillWorking() && !isCompleted() )
{
synchronized ( this )
{
// Only allow a single thread to notify that we've ended our stream as well as calling done()
// stillWorking(), once false cannot again return true so no need to check
if ( !isCompleted() )
{
if ( downstream != null )
{
downstream.endOfUpstream();
}
done();
completed = true;
}
}
}
}
/**
* Called once, when upstream has run out of batches to send and all received batches have been
* processed successfully.
*/
protected void done()
{
endTime = currentTimeMillis();
}
@Override
public void close() throws Exception
{ // Do nothing by default
}
protected void changeName( String name )
{
this.name = name;
}
protected void resetStats()
{
downstreamIdleTime.set( 0 );
upstreamIdleTime.set( 0 );
queuedBatches.set( 0 );
doneBatches.set( 0 );
totalProcessingTime.reset();
startTime = currentTimeMillis();
endTime = 0;
}
@Override
public String toString()
{
return format( "%s[%s, processors:%d, batches:%d", getClass().getSimpleName(),
name, processors( 0 ), doneBatches.get() );
}
@Override
public long doneBatches()
{
return doneBatches.get();
}
}