8
8
import org .apache .log4j .Logger ;
9
9
import org .elasticsearch .ElasticsearchException ;
10
10
import org .elasticsearch .action .index .IndexRequest ;
11
- import org .elasticsearch .common .Numbers ;
12
11
import org .elasticsearch .search .aggregations .Aggregation ;
13
12
import org .elasticsearch .search .aggregations .bucket .composite .CompositeAggregation ;
14
13
import org .elasticsearch .search .aggregations .bucket .histogram .DateHistogramAggregationBuilder ;
21
20
import org .elasticsearch .xpack .core .rollup .job .RollupJobStats ;
22
21
import org .elasticsearch .xpack .rollup .Rollup ;
23
22
24
- import java .nio .charset .StandardCharsets ;
25
23
import java .util .ArrayList ;
26
24
import java .util .HashMap ;
27
25
import java .util .List ;
28
26
import java .util .Map ;
29
27
import java .util .TreeMap ;
30
28
import java .util .stream .Collectors ;
31
- import java .util .zip .CRC32 ;
32
29
33
30
/**
34
31
* These utilities are used to convert agg responses into a set of rollup documents.
@@ -41,12 +38,16 @@ class IndexerUtils {
41
38
* The only entry point in this class. You hand this method an aggregation and an index
42
39
* pattern, and it returns a list of rolled documents that you can index
43
40
*
44
- * @param agg The aggregation response that you want to rollup
45
- * @param rollupIndex The index that holds rollups for this job
41
+ * @param agg The aggregation response that you want to rollup
42
+ * @param rollupIndex The index that holds rollups for this job
43
+ * @param stats The stats accumulator for this job's task
44
+ * @param groupConfig The grouping configuration for the job
45
+ * @param jobId The ID for the job
46
+ * @param isUpgradedDocID `true` if this job is using the new ID scheme
46
47
* @return A list of rolled documents derived from the response
47
48
*/
48
49
static List <IndexRequest > processBuckets (CompositeAggregation agg , String rollupIndex , RollupJobStats stats ,
49
- GroupConfig groupConfig , String jobId ) {
50
+ GroupConfig groupConfig , String jobId , boolean isUpgradedDocID ) {
50
51
51
52
logger .debug ("Buckets: [" + agg .getBuckets ().size () + "][" + jobId + "]" );
52
53
return agg .getBuckets ().stream ().map (b ->{
@@ -57,24 +58,30 @@ static List<IndexRequest> processBuckets(CompositeAggregation agg, String rollup
57
58
TreeMap <String , Object > keys = new TreeMap <>(b .getKey ());
58
59
List <Aggregation > metrics = b .getAggregations ().asList ();
59
60
61
+ RollupIDGenerator idGenerator ;
62
+ if (isUpgradedDocID ) {
63
+ idGenerator = new RollupIDGenerator .Murmur3 (jobId );
64
+ } else {
65
+ idGenerator = new RollupIDGenerator .CRC ();
66
+ }
60
67
Map <String , Object > doc = new HashMap <>(keys .size () + metrics .size ());
61
- CRC32 docId = processKeys ( keys , doc , b . getDocCount (), groupConfig );
62
- byte [] vs = jobId . getBytes ( StandardCharsets . UTF_8 );
63
- docId . update ( vs , 0 , vs . length );
68
+
69
+ processKeys ( keys , doc , b . getDocCount (), groupConfig , idGenerator );
70
+ idGenerator . add ( jobId );
64
71
processMetrics (metrics , doc );
65
72
66
- doc .put (RollupField .ROLLUP_META + "." + RollupField .VERSION_FIELD , Rollup .ROLLUP_VERSION );
73
+ doc .put (RollupField .ROLLUP_META + "." + RollupField .VERSION_FIELD ,
74
+ isUpgradedDocID ? Rollup .CURRENT_ROLLUP_VERSION : Rollup .ROLLUP_VERSION_V1 );
67
75
doc .put (RollupField .ROLLUP_META + "." + RollupField .ID .getPreferredName (), jobId );
68
76
69
- IndexRequest request = new IndexRequest (rollupIndex , RollupField .TYPE_NAME , String . valueOf ( docId . getValue () ));
77
+ IndexRequest request = new IndexRequest (rollupIndex , RollupField .TYPE_NAME , idGenerator . getID ( ));
70
78
request .source (doc );
71
79
return request ;
72
80
}).collect (Collectors .toList ());
73
81
}
74
82
75
- private static CRC32 processKeys (Map <String , Object > keys , Map <String , Object > doc , long count , GroupConfig groupConfig ) {
76
- CRC32 docID = new CRC32 ();
77
-
83
+ private static void processKeys (Map <String , Object > keys , Map <String , Object > doc ,
84
+ long count , GroupConfig groupConfig , RollupIDGenerator idGenerator ) {
78
85
keys .forEach ((k , v ) -> {
79
86
// Also add a doc count for each key. This will duplicate data, but makes search easier later
80
87
doc .put (k + "." + RollupField .COUNT_FIELD , count );
@@ -83,37 +90,34 @@ private static CRC32 processKeys(Map<String, Object> keys, Map<String, Object> d
83
90
assert v != null ;
84
91
doc .put (k + "." + RollupField .TIMESTAMP , v );
85
92
doc .put (k + "." + RollupField .INTERVAL , groupConfig .getDateHisto ().getInterval ());
86
- doc .put (k + "." + DateHistogramGroupConfig .TIME_ZONE , groupConfig .getDateHisto ().getTimeZone ());
87
- docID . update ( Numbers . longToBytes (( Long )v ), 0 , 8 );
93
+ doc .put (k + "." + DateHistogramGroupConfig .TIME_ZONE , groupConfig .getDateHisto ().getTimeZone (). toString () );
94
+ idGenerator . add (( Long )v );
88
95
} else if (k .endsWith ("." + HistogramAggregationBuilder .NAME )) {
89
96
doc .put (k + "." + RollupField .VALUE , v );
90
97
doc .put (k + "." + RollupField .INTERVAL , groupConfig .getHisto ().getInterval ());
91
98
if (v == null ) {
92
- // Arbitrary value to update the doc ID with for nulls
93
- docID .update (19 );
99
+ idGenerator .addNull ();
94
100
} else {
95
- docID . update ( Numbers . doubleToBytes (( Double ) v ), 0 , 8 );
101
+ idGenerator . add (( Double ) v );
96
102
}
97
103
} else if (k .endsWith ("." + TermsAggregationBuilder .NAME )) {
98
104
doc .put (k + "." + RollupField .VALUE , v );
99
105
if (v == null ) {
100
- // Arbitrary value to update the doc ID with for nulls
101
- docID .update (19 );
106
+ idGenerator .addNull ();
102
107
} else if (v instanceof String ) {
103
- byte [] vs = ((String ) v ).getBytes (StandardCharsets .UTF_8 );
104
- docID .update (vs , 0 , vs .length );
108
+ idGenerator .add ((String )v );
105
109
} else if (v instanceof Long ) {
106
- docID . update ( Numbers . longToBytes (( Long )v ), 0 , 8 );
110
+ idGenerator . add (( Long )v );
107
111
} else if (v instanceof Double ) {
108
- docID . update ( Numbers . doubleToBytes (( Double )v ), 0 , 8 );
112
+ idGenerator . add (( Double )v );
109
113
} else {
110
- throw new RuntimeException ("Encountered value of type [" + v .getClass () + "], which was unable to be processed." );
114
+ throw new RuntimeException ("Encountered value of type ["
115
+ + v .getClass () + "], which was unable to be processed." );
111
116
}
112
117
} else {
113
118
throw new ElasticsearchException ("Could not identify key in agg [" + k + "]" );
114
119
}
115
120
});
116
- return docID ;
117
121
}
118
122
119
123
private static void processMetrics (List <Aggregation > metrics , Map <String , Object > doc ) {
0 commit comments