Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Fix #1636, avoid null pointer exception for mapStorageCapacity (#1638)
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE authored Mar 16, 2018
1 parent 66a5239 commit 9326268
Showing 1 changed file with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,23 +399,23 @@ public void insertAccessCountTable(

public void insertUpdateStoragesTable(StorageCapacity[] storages)
throws MetaStoreException {
mapStorageCapacity = null;
try {
storageDao.insertUpdateStoragesTable(storages);
} catch (Exception e) {
throw new MetaStoreException(e);
}
updateCache();
}

public void insertUpdateStoragesTable(List<StorageCapacity> storages)
throws MetaStoreException {
mapStorageCapacity = null;
try {
storageDao.insertUpdateStoragesTable(
storages.toArray(new StorageCapacity[storages.size()]));
} catch (Exception e) {
throw new MetaStoreException(e);
}
updateCache();
}

public void insertUpdateStoragesTable(StorageCapacity storage)
Expand All @@ -424,23 +424,21 @@ public void insertUpdateStoragesTable(StorageCapacity storage)
}

public Map<String, StorageCapacity> getStorageCapacity() throws MetaStoreException {
if (mapStorageCapacity == null) {
updateCache();
}
updateCache();

Map<String, StorageCapacity> ret = new HashMap<>();
if (mapStorageCapacity != null) {
synchronized (storageDao) {
for (String key : mapStorageCapacity.keySet()) {
ret.put(key, mapStorageCapacity.get(key));
}
Map<String, StorageCapacity> currentMapStorageCapacity = mapStorageCapacity;
if (currentMapStorageCapacity != null) {
for (String key : currentMapStorageCapacity.keySet()) {
ret.put(key, currentMapStorageCapacity.get(key));
}
}
return ret;
}

public void deleteStorage(String storageType) throws MetaStoreException {
try {
mapStorageCapacity = null;
storageDao.deleteStorage(storageType);
} catch (Exception e) {
throw new MetaStoreException(e);
Expand All @@ -450,8 +448,17 @@ public void deleteStorage(String storageType) throws MetaStoreException {
public StorageCapacity getStorageCapacity(
String type) throws MetaStoreException {
updateCache();
Map<String, StorageCapacity> currentMapStorageCapacity = mapStorageCapacity;
while (currentMapStorageCapacity == null) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
LOG.error(ex.getMessage());
}
currentMapStorageCapacity = mapStorageCapacity;
}
try {
return mapStorageCapacity.get(type);
return currentMapStorageCapacity.get(type);
} catch (EmptyResultDataAccessException e) {
return null;
} catch (Exception e) {
Expand All @@ -462,6 +469,7 @@ public StorageCapacity getStorageCapacity(
public synchronized boolean updateStoragesTable(String type,
Long capacity, Long free) throws MetaStoreException {
try {
mapStorageCapacity = null;
return storageDao.updateStoragesTable(type, capacity, free);
} catch (Exception e) {
throw new MetaStoreException(e);
Expand Down Expand Up @@ -511,12 +519,13 @@ private void updateCache() throws MetaStoreException {
mapStoragePolicyNameId.put(mapStoragePolicyIdName.get(key), key);
}
}
try {
synchronized (storageDao) {

if (mapStorageCapacity == null) {
try {
mapStorageCapacity = storageDao.getStorageTablesItem();
} catch (Exception e) {
throw new MetaStoreException(e);
}
} catch (Exception e) {
throw new MetaStoreException(e);
}
}

Expand Down

0 comments on commit 9326268

Please sign in to comment.