Skip to content

Commit

Permalink
Bugfix: Fix rackaware placement policy init error (apache#12097)
Browse files Browse the repository at this point in the history
Since the release of Pulsar 2.8 and upgrade to BK 4.12, the default
rackAwarePlacementPolicy has been failing with the following exception:

```
org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicyImpl - Failed to initialize DNS Resolver org.apache.pulsar.zookeeper.ZkBookieRackAffinityMapping, used default subnet resolver : java.lang.RuntimeException: java.lang.NullPointerException java.lang.NullPointerException
```

This regression occured in commit apache@4c60262

The core of the issue is that `setConf` is called before
`setBookieAddressResolver` has been set (see
https://github.com/apache/bookkeeper/blob/034ef8566ad037937a4d58a28f70631175744f53/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/RackawareEnsemblePlacementPolicyImpl.java#L264-L286)

This results in the NPE.

We are safe to simply not eagerly init the cache in setConf as the
getRack call will re-check the cache.

We also protect against this possible NPE for safety

(cherry picked from commit 9975fe4)
  • Loading branch information
addisonj authored and nicoloboschi committed Oct 20, 2021
1 parent 78984a5 commit f34a324
Showing 1 changed file with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,8 @@ public void setConf(Configuration conf) {
conf.setProperty(ZK_DATA_CACHE_BK_RACK_CONF_INSTANCE, bookieMappingCache);
}

try {
BookiesRackConfiguration racks = bookieMappingCache.get(BOOKIE_INFO_ROOT_PATH).orElse(new BookiesRackConfiguration());
updateRacksWithHost(racks);
} catch (Exception e) {
throw new RuntimeException(e);
}
// A previous version of this code tried to eagerly load the cache. However, this is invalid
// in later versions of bookkeeper as when setConf is called, the bookieAddressResolver is not yet set
}

private void updateRacksWithHost(BookiesRackConfiguration racks) {
Expand All @@ -94,20 +90,25 @@ private void updateRacksWithHost(BookiesRackConfiguration racks) {
bookies.forEach((addr, bi) -> {
try {
BookieId bookieId = BookieId.parse(addr);
BookieSocketAddress bsa = getBookieAddressResolver().resolve(bookieId);
newRacksWithHost.updateBookie(group, bsa.toString(), bi);

String hostname = bsa.getSocketAddress().getHostName();
newBookieInfoMap.put(hostname, bi);

InetAddress address = bsa.getSocketAddress().getAddress();
if (null != address) {
String hostIp = address.getHostAddress();
if (null != hostIp) {
newBookieInfoMap.put(hostIp, bi);
}
BookieAddressResolver addressResolver = getBookieAddressResolver();
if (addressResolver == null) {
LOG.warn("Bookie address resolver not yet initialized, skipping resolution");
} else {
LOG.info("Network address for {} is unresolvable yet.", addr);
BookieSocketAddress bsa = addressResolver.resolve(bookieId);
newRacksWithHost.updateBookie(group, bsa.toString(), bi);

String hostname = bsa.getSocketAddress().getHostName();
newBookieInfoMap.put(hostname, bi);

InetAddress address = bsa.getSocketAddress().getAddress();
if (null != address) {
String hostIp = address.getHostAddress();
if (null != hostIp) {
newBookieInfoMap.put(hostIp, bi);
}
} else {
LOG.info("Network address for {} is unresolvable yet.", addr);
}
}
} catch (BookieAddressResolver.BookieIdNotResolvedException e) {
LOG.info("Network address for {} is unresolvable yet. error is {}", addr, e);
Expand Down

0 comments on commit f34a324

Please sign in to comment.