23
23
import java .util .Map ;
24
24
import java .util .Map .Entry ;
25
25
import java .util .TreeSet ;
26
+ import java .util .function .Consumer ;
26
27
27
28
import org .apache .accumulo .core .client .Accumulo ;
28
29
import org .apache .accumulo .core .client .AccumuloClient ;
45
46
import io .opentelemetry .context .Scope ;
46
47
47
48
public class CheckForMetadataProblems {
48
- private static boolean sawProblems = false ;
49
- private static ServerUtilOpts opts ;
50
49
51
- private static void checkTable (TableId tableId , TreeSet <KeyExtent > tablets ) {
50
+ private static boolean checkTable (TableId tableId , TreeSet <KeyExtent > tablets ,
51
+ ServerUtilOpts opts , Consumer <String > printInfoMethod , Consumer <String > printProblemMethod ) {
52
52
// sanity check of metadata table entries
53
- // make sure tablets has no holes, and that it starts and ends w/ null
53
+ // make sure tablets have no holes, and that it starts and ends w/ null
54
54
String tableName ;
55
+ boolean sawProblems = false ;
55
56
56
57
try {
57
58
tableName = opts .getServerContext ().getTableName (tableId );
58
59
} catch (TableNotFoundException e ) {
59
60
tableName = null ;
60
61
}
61
62
63
+ printInfoMethod .accept (String .format ("Ensuring tablets for table %s (%s) have: no holes, "
64
+ + "valid (null) prev end row for first tablet, and valid (null) end row "
65
+ + "for last tablet...\n " , tableName , tableId ));
66
+
62
67
if (tablets .isEmpty ()) {
63
- System .out .println (
64
- "...No entries found in metadata table for table " + tableName + " (" + tableId + ")" );
65
- sawProblems = true ;
66
- return ;
68
+ printProblemMethod .accept (String
69
+ .format ("...No entries found in metadata table for table %s (%s)" , tableName , tableId ));
70
+ return true ;
67
71
}
68
72
69
73
if (tablets .first ().prevEndRow () != null ) {
70
- System . out . println ( "...First entry for table " + tableName + " (" + tableId + ") - "
71
- + tablets . first () + " - has non null prev end row");
72
- sawProblems = true ;
73
- return ;
74
+ printProblemMethod
75
+ . accept ( String . format ( "...First entry for table %s (%s) - %s - has non- null prev end row",
76
+ tableName , tableId , tablets . first ())) ;
77
+ return true ;
74
78
}
75
79
76
80
if (tablets .last ().endRow () != null ) {
77
- System . out . println ( "...Last entry for table " + tableName + " (" + tableId + ") - "
78
- + tablets . last () + " - has non null end row");
79
- sawProblems = true ;
80
- return ;
81
+ printProblemMethod
82
+ . accept ( String . format ( "...Last entry for table %s (%s) - %s - has non- null end row",
83
+ tableName , tableId , tablets . last ())) ;
84
+ return true ;
81
85
}
82
86
83
87
Iterator <KeyExtent > tabIter = tablets .iterator ();
84
88
Text lastEndRow = tabIter .next ().endRow ();
85
89
boolean everythingLooksGood = true ;
86
90
while (tabIter .hasNext ()) {
87
- KeyExtent tabke = tabIter .next ();
91
+ KeyExtent table = tabIter .next ();
88
92
boolean broke = false ;
89
- if (tabke .prevEndRow () == null ) {
90
- System .out .println ("...Table " + tableName + " (" + tableId
91
- + ") has null prev end row in middle of table " + tabke );
93
+ if (table .prevEndRow () == null ) {
94
+ printProblemMethod
95
+ .accept (String .format ("...Table %s (%s) has null prev end row in middle of table %s" ,
96
+ tableName , tableId , table ));
92
97
broke = true ;
93
- } else if (!tabke .prevEndRow ().equals (lastEndRow )) {
94
- System . out . println ("...Table " + tableName + " (" + tableId + " ) has a hole "
95
- + tabke .prevEndRow () + " != " + lastEndRow );
98
+ } else if (!table .prevEndRow ().equals (lastEndRow )) {
99
+ printProblemMethod . accept ( String . format ("...Table %s (%s ) has a hole %s != %s" , tableName ,
100
+ tableId , table .prevEndRow (), lastEndRow ) );
96
101
broke = true ;
97
102
}
98
103
if (broke ) {
99
104
everythingLooksGood = false ;
100
105
}
101
106
102
- lastEndRow = tabke .endRow ();
107
+ lastEndRow = table .endRow ();
103
108
}
104
109
if (everythingLooksGood ) {
105
- System . out . println ("...All is well for table " + tableName + " (" + tableId + ")" );
110
+ printInfoMethod . accept ( String . format ("...All is well for table %s (%s)" , tableName , tableId ) );
106
111
} else {
107
112
sawProblems = true ;
108
113
}
114
+
115
+ return sawProblems ;
109
116
}
110
117
111
- private static void checkMetadataAndRootTableEntries (String tableNameToCheck , ServerUtilOpts opts )
118
+ public static boolean checkMetadataAndRootTableEntries (String tableNameToCheck ,
119
+ ServerUtilOpts opts , Consumer <String > printInfoMethod , Consumer <String > printProblemMethod )
112
120
throws Exception {
113
121
TableId tableCheckId = opts .getServerContext ().getTableId (tableNameToCheck );
114
- System . out . println ("Checking tables whose metadata is found in: " + tableNameToCheck + " ("
115
- + tableCheckId + ")" );
122
+ printInfoMethod . accept ( String . format ("Checking tables whose metadata is found in: %s (%s)... \n " ,
123
+ tableNameToCheck , tableCheckId ) );
116
124
Map <TableId ,TreeSet <KeyExtent >> tables = new HashMap <>();
125
+ boolean sawProblems = false ;
117
126
118
127
try (AccumuloClient client = Accumulo .newClient ().from (opts .getClientProps ()).build ();
119
128
Scanner scanner = client .createScanner (tableNameToCheck , Authorizations .EMPTY )) {
@@ -139,7 +148,10 @@ private static void checkMetadataAndRootTableEntries(String tableNameToCheck, Se
139
148
TreeSet <KeyExtent > tablets = tables .get (tableId );
140
149
if (tablets == null ) {
141
150
142
- tables .forEach (CheckForMetadataProblems ::checkTable );
151
+ for (var e : tables .entrySet ()) {
152
+ sawProblems = CheckForMetadataProblems .checkTable (e .getKey (), e .getValue (), opts ,
153
+ printInfoMethod , printProblemMethod ) || sawProblems ;
154
+ }
143
155
144
156
tables .clear ();
145
157
@@ -153,37 +165,45 @@ private static void checkMetadataAndRootTableEntries(String tableNameToCheck, Se
153
165
justLoc = false ;
154
166
} else if (colf .equals (CurrentLocationColumnFamily .NAME )) {
155
167
if (justLoc ) {
156
- System . out . println ("Problem at key " + entry .getKey ());
168
+ printProblemMethod . accept ("Problem at key " + entry .getKey ());
157
169
sawProblems = true ;
158
170
}
159
171
justLoc = true ;
160
172
}
161
173
}
162
174
163
175
if (count == 0 ) {
164
- System . err
165
- . println ("ERROR : table " + tableNameToCheck + " (" + tableCheckId + " ) is empty" );
176
+ printProblemMethod . accept (
177
+ String . format ("ERROR : table %s (%s ) is empty" , tableNameToCheck , tableCheckId ) );
166
178
sawProblems = true ;
167
179
}
168
180
}
169
181
170
- tables .forEach (CheckForMetadataProblems ::checkTable );
182
+ for (var e : tables .entrySet ()) {
183
+ sawProblems = CheckForMetadataProblems .checkTable (e .getKey (), e .getValue (), opts ,
184
+ printInfoMethod , printProblemMethod ) || sawProblems ;
185
+ }
171
186
172
187
if (!sawProblems ) {
173
- System .out .println ("No problems found in " + tableNameToCheck + " (" + tableCheckId + ")" );
188
+ printInfoMethod .accept (
189
+ String .format ("\n ...No problems found in %s (%s)" , tableNameToCheck , tableCheckId ));
174
190
}
175
191
// end METADATA table sanity check
192
+ return sawProblems ;
176
193
}
177
194
178
195
public static void main (String [] args ) throws Exception {
179
- opts = new ServerUtilOpts ();
196
+ ServerUtilOpts opts = new ServerUtilOpts ();
180
197
opts .parseArgs (CheckForMetadataProblems .class .getName (), args );
181
198
Span span = TraceUtil .startSpan (CheckForMetadataProblems .class , "main" );
199
+ boolean sawProblems ;
182
200
try (Scope scope = span .makeCurrent ()) {
183
201
184
- checkMetadataAndRootTableEntries (AccumuloTable .ROOT .tableName (), opts );
202
+ sawProblems = checkMetadataAndRootTableEntries (AccumuloTable .ROOT .tableName (), opts ,
203
+ System .out ::println , System .out ::println );
185
204
System .out .println ();
186
- checkMetadataAndRootTableEntries (AccumuloTable .METADATA .tableName (), opts );
205
+ sawProblems = checkMetadataAndRootTableEntries (AccumuloTable .METADATA .tableName (), opts ,
206
+ System .out ::println , System .out ::println ) || sawProblems ;
187
207
if (sawProblems ) {
188
208
throw new IllegalStateException ();
189
209
}
0 commit comments