diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/MetaStore.java b/smart-metastore/src/main/java/org/smartdata/metastore/MetaStore.java index df39bc8bef4..8ec5fcf2423 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/MetaStore.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/MetaStore.java @@ -398,23 +398,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 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) @@ -429,8 +429,10 @@ public Map getStorageCapacity() throws MetaStoreExcepti Map ret = new HashMap<>(); if (mapStorageCapacity != null) { - for (String key : mapStorageCapacity.keySet()) { - ret.put(key, mapStorageCapacity.get(key)); + synchronized (storageDao) { + for (String key : mapStorageCapacity.keySet()) { + ret.put(key, mapStorageCapacity.get(key)); + } } } return ret; @@ -508,12 +510,12 @@ private void updateCache() throws MetaStoreException { mapStoragePolicyNameId.put(mapStoragePolicyIdName.get(key), key); } } - if (mapStorageCapacity == null) { - try { + try { + synchronized (storageDao) { mapStorageCapacity = storageDao.getStorageTablesItem(); - } catch (Exception e) { - throw new MetaStoreException(e); } + } catch (Exception e) { + throw new MetaStoreException(e); } } diff --git a/smart-metastore/src/test/java/org/smartdata/metastore/TestMetaStore.java b/smart-metastore/src/test/java/org/smartdata/metastore/TestMetaStore.java index 0a67cefb7c4..1881d4c8654 100644 --- a/smart-metastore/src/test/java/org/smartdata/metastore/TestMetaStore.java +++ b/smart-metastore/src/test/java/org/smartdata/metastore/TestMetaStore.java @@ -211,7 +211,7 @@ public void testInsertStoragesTable() throws Exception { Assert.assertTrue(storageCapacity1.equals(storage1)); Assert.assertTrue(storageCapacity2.equals(storage2)); Assert.assertTrue(metaStore.updateStoragesTable("Flash", 123456L, 4562233L)); - Assert.assertTrue(metaStore.getStorageCapacity("Flash").getCapacity() == 12343333L); + Assert.assertTrue(metaStore.getStorageCapacity("Flash").getCapacity() == 123456L); } @Test