@@ -70,7 +70,7 @@ public class BookieRackAffinityMapping extends AbstractDNSToSwitchMapping
70
70
private BookiesRackConfiguration racksWithHost = new BookiesRackConfiguration ();
71
71
private Map <String , BookieInfo > bookieInfoMap = new HashMap <>();
72
72
73
- public static MetadataStore createMetadataStore (Configuration conf ) throws MetadataException {
73
+ static MetadataStore getMetadataStore (Configuration conf ) throws MetadataException {
74
74
MetadataStore store ;
75
75
Object storeProperty = conf .getProperty (METADATA_STORE_INSTANCE );
76
76
if (storeProperty != null ) {
@@ -116,12 +116,20 @@ public synchronized void setConf(Configuration conf) {
116
116
super .setConf (conf );
117
117
MetadataStore store ;
118
118
try {
119
- store = createMetadataStore (conf );
120
- bookieMappingCache = store .getMetadataCache (BookiesRackConfiguration .class );
121
- store .registerListener (this ::handleUpdates );
122
- racksWithHost = bookieMappingCache .get (BOOKIE_INFO_ROOT_PATH ).get ()
123
- .orElseGet (BookiesRackConfiguration ::new );
124
- for (Map <String , BookieInfo > bookieMapping : racksWithHost .values ()) {
119
+ store = getMetadataStore (conf );
120
+ } catch (MetadataException e ) {
121
+ throw new RuntimeException (METADATA_STORE_INSTANCE + " failed to init BookieId list" );
122
+ }
123
+
124
+ bookieMappingCache = store .getMetadataCache (BookiesRackConfiguration .class );
125
+ store .registerListener (this ::handleUpdates );
126
+
127
+ try {
128
+ var racksWithHost = bookieMappingCache .get (BOOKIE_INFO_ROOT_PATH )
129
+ .thenApply (optRes -> optRes .orElseGet (BookiesRackConfiguration ::new ))
130
+ .get ();
131
+
132
+ for (var bookieMapping : racksWithHost .values ()) {
125
133
for (String address : bookieMapping .keySet ()) {
126
134
bookieAddressListLastTime .add (BookieId .parse (address ));
127
135
}
@@ -131,10 +139,12 @@ public synchronized void setConf(Configuration conf) {
131
139
}
132
140
}
133
141
updateRacksWithHost (racksWithHost );
134
- watchAvailableBookies ();
135
- } catch ( InterruptedException | ExecutionException | MetadataException e ) {
136
- throw new RuntimeException (METADATA_STORE_INSTANCE + " failed to init BookieId list" );
142
+ } catch ( ExecutionException | InterruptedException e ) {
143
+ LOG . error ( "Failed to update rack info. " , e );
144
+ throw new RuntimeException (e );
137
145
}
146
+
147
+ watchAvailableBookies ();
138
148
}
139
149
140
150
private void watchAvailableBookies () {
@@ -145,13 +155,13 @@ private void watchAvailableBookies() {
145
155
field .setAccessible (true );
146
156
RegistrationClient registrationClient = (RegistrationClient ) field .get (bookieAddressResolver );
147
157
registrationClient .watchWritableBookies (versioned -> {
148
- try {
149
- racksWithHost = bookieMappingCache . get ( BOOKIE_INFO_ROOT_PATH ). get ( )
150
- . orElseGet ( BookiesRackConfiguration :: new );
151
- updateRacksWithHost ( racksWithHost );
152
- } catch ( InterruptedException | ExecutionException e ) {
153
- LOG . error ( "Failed to update rack info. " , e ) ;
154
- }
158
+ bookieMappingCache . get ( BOOKIE_INFO_ROOT_PATH )
159
+ . thenApply ( optRes -> optRes . orElseGet ( BookiesRackConfiguration :: new ) )
160
+ . thenAccept ( this :: updateRacksWithHost )
161
+ . exceptionally ( ex -> {
162
+ LOG . error ( "Failed to update rack info. " , ex );
163
+ return null ;
164
+ });
155
165
});
156
166
} catch (NoSuchFieldException | IllegalAccessException e ) {
157
167
LOG .error ("Failed watch available bookies." , e );
0 commit comments