-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BadCollector.java
284 lines (246 loc) · 8.53 KB
/
BadCollector.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (c) 2002-2018 "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.input;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.concurrent.atomic.AtomicLong;
import org.neo4j.concurrent.AsyncEvent;
import org.neo4j.concurrent.AsyncEvents;
import org.neo4j.unsafe.impl.batchimport.cache.idmapping.string.DuplicateInputIdException;
import static java.lang.String.format;
import static org.neo4j.helpers.Exceptions.withMessage;
public class BadCollector implements Collector
{
/**
* Introduced to avoid creating an exception for every reported bad thing, since it can be
* quite the performance hogger for scenarios where there are many many bad things to collect.
*/
abstract static class ProblemReporter extends AsyncEvent
{
private final int type;
ProblemReporter( int type )
{
this.type = type;
}
int type()
{
return type;
}
abstract String message();
abstract InputException exception();
}
public static final int BAD_RELATIONSHIPS = 0x1;
public static final int DUPLICATE_NODES = 0x2;
public static final int EXTRA_COLUMNS = 0x4;
public static final int COLLECT_ALL = BAD_RELATIONSHIPS | DUPLICATE_NODES | EXTRA_COLUMNS;
public static final long UNLIMITED_TOLERANCE = -1;
private final PrintStream out;
private final long tolerance;
private final int collect;
private final boolean logBadEntries;
// volatile since one importer thread calls collect(), where this value is incremented and later the "main"
// thread calls badEntries() to get a count.
private final AtomicLong badEntries = new AtomicLong();
private final AsyncEvents<ProblemReporter> logger;
private final Thread eventProcessor;
public BadCollector( OutputStream out, long tolerance, int collect )
{
this( out, tolerance, collect, false );
}
public BadCollector( OutputStream out, long tolerance, int collect, boolean skipBadEntriesLogging )
{
this.out = new PrintStream( out );
this.tolerance = tolerance;
this.collect = collect;
this.logBadEntries = !skipBadEntriesLogging;
this.logger = new AsyncEvents<>( this::processEvent, AsyncEvents.Monitor.NONE );
this.eventProcessor = new Thread( logger );
this.eventProcessor.start();
}
private void processEvent( ProblemReporter report )
{
out.println( report.message() );
}
@Override
public void collectBadRelationship( Object startId, String startIdGroup, String type, Object endId,
String endIdGroup, Object specificValue )
{
collect( new RelationshipsProblemReporter( startId, startIdGroup, type, endId, endIdGroup, specificValue ) );
}
@Override
public void collectExtraColumns( final String source, final long row, final String value )
{
collect( new ExtraColumnsProblemReporter( row, source, value ) );
}
@Override
public void collectDuplicateNode( final Object id, long actualId, final String group )
{
collect( new NodesProblemReporter( id, group ) );
}
@Override
public boolean isCollectingBadRelationships()
{
return collects( BAD_RELATIONSHIPS );
}
private void collect( ProblemReporter report )
{
boolean collect = collects( report.type() );
if ( collect )
{
// This type of problem is collected and we're within the max threshold, so it's OK
long count = badEntries.incrementAndGet();
if ( tolerance == UNLIMITED_TOLERANCE || count <= tolerance )
{
// We're within the threshold
if ( logBadEntries )
{
// Send this to the logger
logger.send( report );
}
return; // i.e. don't treat this as an exception
}
}
InputException exception = report.exception();
throw collect ? withMessage( exception, format( "Too many bad entries %d, where last one was: %s",
badEntries.longValue(), exception.getMessage() ) ) : exception;
}
@Override
public void close()
{
logger.shutdown();
try
{
logger.awaitTermination();
}
finally
{
out.flush();
}
}
@Override
public long badEntries()
{
return badEntries.get();
}
private boolean collects( int bit )
{
return (collect & bit) != 0;
}
private static class RelationshipsProblemReporter extends ProblemReporter
{
private String message;
private final Object specificValue;
private final Object startId;
private final String startIdGroup;
private final String type;
private final Object endId;
private final String endIdGroup;
RelationshipsProblemReporter( Object startId, String startIdGroup, String type,
Object endId, String endIdGroup, Object specificValue )
{
super( BAD_RELATIONSHIPS );
this.startId = startId;
this.startIdGroup = startIdGroup;
this.type = type;
this.endId = endId;
this.endIdGroup = endIdGroup;
this.specificValue = specificValue;
}
@Override
public String message()
{
return getReportMessage();
}
@Override
public InputException exception()
{
return new InputException( getReportMessage() );
}
private String getReportMessage()
{
if ( message == null )
{
message = !isMissingData()
? format( "%s (%s)-[%s]->%s (%s) referring to missing node %s",
startId, startIdGroup, type, endId, endIdGroup, specificValue )
: format( "%s (%s)-[%s]->%s (%s) is missing data",
startId, startIdGroup, type, endId, endIdGroup );
}
return message;
}
private boolean isMissingData()
{
return startId == null || endId == null || type == null;
}
}
private static class NodesProblemReporter extends ProblemReporter
{
private final Object id;
private final String group;
NodesProblemReporter( Object id, String group )
{
super( DUPLICATE_NODES );
this.id = id;
this.group = group;
}
@Override
public String message()
{
return DuplicateInputIdException.message( id, group );
}
@Override
public InputException exception()
{
return new DuplicateInputIdException( id, group );
}
}
private static class ExtraColumnsProblemReporter extends ProblemReporter
{
private String message;
private final long row;
private final String source;
private final String value;
ExtraColumnsProblemReporter( long row, String source, String value )
{
super( EXTRA_COLUMNS );
this.row = row;
this.source = source;
this.value = value;
}
@Override
public String message()
{
return getReportMessage();
}
@Override
public InputException exception()
{
return new InputException( getReportMessage() );
}
private String getReportMessage()
{
if ( message == null )
{
message = format( "Extra column not present in header on line %d in %s with value %s", row, source, value );
}
return message;
}
}
}