17
17
* You should have received a copy of the GNU General Public License
18
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
*/
20
- package org .neo4j .gds .core .utils ;
20
+ package org .neo4j .gds .core .utils . progress ;
21
21
22
22
import org .assertj .core .api .junit .jupiter .SoftAssertionsExtension ;
23
23
import org .eclipse .collections .impl .utility .ListIterate ;
27
27
import org .neo4j .gds .compat .TestLog ;
28
28
import org .neo4j .gds .core .concurrency .Concurrency ;
29
29
import org .neo4j .gds .core .concurrency .RunWithConcurrency ;
30
- import org .neo4j .gds .core .utils .progress .BatchingProgressLogger ;
30
+ import org .neo4j .gds .core .utils .progress .tasks . LeafTask ;
31
31
import org .neo4j .gds .core .utils .progress .tasks .Tasks ;
32
32
import org .neo4j .gds .logging .GdsTestLog ;
33
33
import org .neo4j .gds .logging .Log ;
46
46
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
47
47
import static org .assertj .core .api .Assertions .fail ;
48
48
import static org .junit .jupiter .api .Assertions .assertEquals ;
49
+ import static org .mockito .Mockito .mock ;
50
+ import static org .mockito .Mockito .verify ;
51
+ import static org .mockito .Mockito .when ;
49
52
import static org .neo4j .gds .assertj .Extractors .removingThreadId ;
50
53
import static org .neo4j .gds .utils .StringFormatting .formatWithLocale ;
51
54
@@ -60,6 +63,7 @@ void mustLogProgressOnlyAfterBatchSizeInvocations() {
60
63
var concurrency = new Concurrency (1 );
61
64
var logger = new BatchingProgressLogger (
62
65
log ,
66
+ new JobId ("some job id" ),
63
67
Tasks .leaf ("foo" , taskVolume ),
64
68
batchSize ,
65
69
concurrency
@@ -71,13 +75,13 @@ void mustLogProgressOnlyAfterBatchSizeInvocations() {
71
75
}
72
76
73
77
var threadName = Thread .currentThread ().getName ();
74
- var messageTemplate = "[%s] foo %d%% %d" ;
78
+ var messageTemplate = "[%s] [%s] foo %d%% %d" ;
75
79
var expectedMessages = List .of (
76
- formatWithLocale (messageTemplate , threadName , 1 * batchSize * 100 / taskVolume , 1 * batchSize - 1 ),
77
- formatWithLocale (messageTemplate , threadName , 2 * batchSize * 100 / taskVolume , 2 * batchSize - 1 ),
78
- formatWithLocale (messageTemplate , threadName , 3 * batchSize * 100 / taskVolume , 3 * batchSize - 1 ),
79
- formatWithLocale (messageTemplate , threadName , 4 * batchSize * 100 / taskVolume , 4 * batchSize - 1 ),
80
- formatWithLocale (messageTemplate , threadName , 5 * batchSize * 100 / taskVolume , 5 * batchSize - 1 )
80
+ formatWithLocale (messageTemplate , "some job id" , threadName , 1 * batchSize * 100 / taskVolume , 1 * batchSize - 1 ),
81
+ formatWithLocale (messageTemplate , "some job id" , threadName , 2 * batchSize * 100 / taskVolume , 2 * batchSize - 1 ),
82
+ formatWithLocale (messageTemplate , "some job id" , threadName , 3 * batchSize * 100 / taskVolume , 3 * batchSize - 1 ),
83
+ formatWithLocale (messageTemplate , "some job id" , threadName , 4 * batchSize * 100 / taskVolume , 4 * batchSize - 1 ),
84
+ formatWithLocale (messageTemplate , "some job id" , threadName , 5 * batchSize * 100 / taskVolume , 5 * batchSize - 1 )
81
85
);
82
86
83
87
var messages = log .getMessages ("info" );
@@ -93,6 +97,7 @@ void mustLogProgressOnlyAfterHittingOrExceedingBatchSize() {
93
97
var concurrency = new Concurrency (1 );
94
98
var logger = new BatchingProgressLogger (
95
99
log ,
100
+ new JobId ("a job id" ),
96
101
Tasks .leaf ("foo" , taskVolume ),
97
102
batchSize ,
98
103
concurrency
@@ -103,7 +108,7 @@ void mustLogProgressOnlyAfterHittingOrExceedingBatchSize() {
103
108
}
104
109
105
110
var threadName = Thread .currentThread ().getName ();
106
- var messageTemplate = "[%s] foo %d%%" ;
111
+ var messageTemplate = "[%s] [%s] foo %d%%" ;
107
112
108
113
var progressSteps = IntStream
109
114
.iterate (0 , i -> i < taskVolume , i -> i + progressStep )
@@ -113,7 +118,7 @@ void mustLogProgressOnlyAfterHittingOrExceedingBatchSize() {
113
118
114
119
var expectedMessages = loggedProgressSteps .stream ()
115
120
.skip (1 )
116
- .map (i -> formatWithLocale (messageTemplate , threadName , i * 100 / taskVolume ))
121
+ .map (i -> formatWithLocale (messageTemplate , "a job id" , threadName , i * 100 / taskVolume ))
117
122
.collect (Collectors .toList ());
118
123
119
124
var messages = log .getMessages ("info" );
@@ -150,7 +155,7 @@ void shouldLogAfterResetWhereACallCountHigherThanBatchSizeIsLeftBehind() {
150
155
var taskVolume = 1337 ;
151
156
152
157
var log = new GdsTestLog ();
153
- var logger = new BatchingProgressLogger (log , Tasks .leaf ("Test" , taskVolume ), concurrency ); // batchSize is 13
158
+ var logger = new BatchingProgressLogger (log , new JobId (), Tasks .leaf ("Test" , taskVolume ), concurrency ); // batchSize is 13
154
159
logger .reset (taskVolume );
155
160
logger .logProgress (20 ); // callCount is 20, call count after logging == 20 - 13 = 7
156
161
assertThat (log .getMessages (TestLog .INFO ))
@@ -167,7 +172,7 @@ void shouldLogAfterResetWhereACallCountHigherThanBatchSizeIsLeftBehind() {
167
172
void log100Percent () {
168
173
var log = new GdsTestLog ();
169
174
var concurrency = new Concurrency (1 );
170
- var testProgressLogger = new BatchingProgressLogger (log , Tasks .leaf ("Test" ), concurrency );
175
+ var testProgressLogger = new BatchingProgressLogger (log , new JobId (), Tasks .leaf ("Test" ), concurrency );
171
176
testProgressLogger .reset (1337 );
172
177
testProgressLogger .logFinishPercentage ();
173
178
assertThat (log .getMessages (TestLog .INFO ))
@@ -179,7 +184,7 @@ void log100Percent() {
179
184
void shouldLog100OnlyOnce () {
180
185
var log = new GdsTestLog ();
181
186
var concurrency = new Concurrency (1 );
182
- var testProgressLogger = new BatchingProgressLogger (log , Tasks .leaf ("Test" ), concurrency );
187
+ var testProgressLogger = new BatchingProgressLogger (log , new JobId (), Tasks .leaf ("Test" ), concurrency );
183
188
testProgressLogger .reset (1 );
184
189
testProgressLogger .logProgress (1 );
185
190
testProgressLogger .logFinishPercentage ();
@@ -192,7 +197,7 @@ void shouldLog100OnlyOnce() {
192
197
void shouldNotExceed100Percent () {
193
198
var log = new GdsTestLog ();
194
199
var concurrency = new Concurrency (1 );
195
- var testProgressLogger = new BatchingProgressLogger (log , Tasks .leaf ("Test" ), concurrency );
200
+ var testProgressLogger = new BatchingProgressLogger (log , new JobId (), Tasks .leaf ("Test" ), concurrency );
196
201
testProgressLogger .reset (1 );
197
202
testProgressLogger .logProgress (1 ); // reaches 100 %
198
203
testProgressLogger .logProgress (1 ); // exceeds 100 %
@@ -205,6 +210,7 @@ void shouldNotExceed100Percent() {
205
210
void closesThreadLocal () {
206
211
var logger = new BatchingProgressLogger (
207
212
Log .noOpLog (),
213
+ new JobId (),
208
214
Tasks .leaf ("foo" , 42 ),
209
215
new Concurrency (1 )
210
216
);
@@ -226,7 +232,7 @@ void closesThreadLocal() {
226
232
227
233
private static List <Integer > performLogging (long taskVolume , Concurrency concurrency ) {
228
234
var log = new GdsTestLog ();
229
- var logger = new BatchingProgressLogger (log , Tasks .leaf ("Test" , taskVolume ), concurrency );
235
+ var logger = new BatchingProgressLogger (log , new JobId ( "the_job_id" ), Tasks .leaf ("Test" , taskVolume ), concurrency );
230
236
logger .reset (taskVolume );
231
237
232
238
var batchSize = (int ) BitUtil .ceilDiv (taskVolume , concurrency .value ());
@@ -248,9 +254,69 @@ private static List<Integer> performLogging(long taskVolume, Concurrency concurr
248
254
return log
249
255
.getMessages (TestLog .INFO )
250
256
.stream ()
251
- .map (progress -> progress .split (" " )[2 ].replace ("%" , "" ))
257
+ .map (progress -> progress .split (" " )[3 ].replace ("%" , "" ))
252
258
.map (Integer ::parseInt )
253
259
.collect (Collectors .toList ());
254
260
}
255
261
262
+ @ Test
263
+ void shouldPrependCorrelationIdToInfoLogMessages () {
264
+ var log = mock (Log .class );
265
+ var batchingProgressLogger = new BatchingProgressLogger (
266
+ log ,
267
+ JobId .parse ("my job id" ),
268
+ new LeafTask ("Monsieur Alfonse" , 42 ),
269
+ new Concurrency (87 )
270
+ );
271
+
272
+ batchingProgressLogger .logMessage ("Swiftly, and with style" );
273
+
274
+ verify (log ).info ("[%s] [%s] %s %s" , "my job id" , "Test worker" , "Monsieur Alfonse" , "Swiftly, and with style" );
275
+ }
276
+
277
+ @ Test
278
+ void shouldPrependCorrelationIdToDebugLogMessages () {
279
+ var log = mock (Log .class );
280
+ var batchingProgressLogger = new BatchingProgressLogger (
281
+ log ,
282
+ JobId .parse ("my job id" ),
283
+ new LeafTask ("Monsieur Alfonse" , 42 ),
284
+ new Concurrency (87 )
285
+ );
286
+
287
+ when (log .isDebugEnabled ()).thenReturn (true );
288
+ batchingProgressLogger .logDebug ("Swiftly, and with style" );
289
+
290
+ verify (log ).debug ("[%s] [%s] %s %s" , "my job id" , "Test worker" , "Monsieur Alfonse" , "Swiftly, and with style" );
291
+ }
292
+
293
+ @ Test
294
+ void shouldPrependCorrelationIdToWarningLogMessages () {
295
+ var log = mock (Log .class );
296
+ var batchingProgressLogger = new BatchingProgressLogger (
297
+ log ,
298
+ JobId .parse ("my job id" ),
299
+ new LeafTask ("Monsieur Alfonse" , 42 ),
300
+ new Concurrency (87 )
301
+ );
302
+
303
+ batchingProgressLogger .logWarning ("Swiftly, and with style" );
304
+
305
+ verify (log ).warn ("[%s] [%s] %s %s" , "my job id" , "Test worker" , "Monsieur Alfonse" , "Swiftly, and with style" );
306
+ }
307
+
308
+ @ Test
309
+ void shouldPrependCorrelationIdToErrorLogMessages () {
310
+ var log = mock (Log .class );
311
+ var batchingProgressLogger = new BatchingProgressLogger (
312
+ log ,
313
+ JobId .parse ("my job id" ),
314
+ new LeafTask ("Monsieur Alfonse" , 42 ),
315
+ new Concurrency (87 )
316
+ );
317
+
318
+ batchingProgressLogger .logError ("Swiftly, and with style" );
319
+
320
+ verify (log ).error ("[%s] [%s] %s %s" , "my job id" , "Test worker" , "Monsieur Alfonse" , "Swiftly, and with style" );
321
+ }
256
322
}
0 commit comments