From bde5d06fd475e9c8a3590f2e13998111a4a7d97a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Boschi?= Date: Tue, 30 Apr 2024 10:42:47 +0200 Subject: [PATCH 1/4] fix: deleted ledgers are not removed from the cache --- .../main/java/org/bkvm/auth/AuthManager.java | 2 +- web/src/main/java/org/bkvm/auth/UserRole.java | 15 +++-- .../bookkeeper/BookkeeperClusterPool.java | 2 +- .../bkvm/bookkeeper/BookkeeperManager.java | 7 +- .../java/org/bkvm/cache/MetadataCache.java | 15 ++--- .../org/bkvm/cache/LoadMetadataCacheTest.java | 64 +++++++++++++++++++ .../org/bkvm/cache/MetadataCacheTest.java | 6 +- .../utils/BookkeeperManagerTestUtils.java | 11 ++-- 8 files changed, 93 insertions(+), 29 deletions(-) create mode 100644 web/src/test/java/org/bkvm/cache/LoadMetadataCacheTest.java diff --git a/web/src/main/java/org/bkvm/auth/AuthManager.java b/web/src/main/java/org/bkvm/auth/AuthManager.java index 2661dfa9..4cc5825a 100644 --- a/web/src/main/java/org/bkvm/auth/AuthManager.java +++ b/web/src/main/java/org/bkvm/auth/AuthManager.java @@ -57,7 +57,7 @@ public AuthManager(ConfigurationStore store) { } private boolean checkUserRole(String role) { - return EnumUtils.isValidEnum(UserRole.class, role); + return UserRole.Fields.isValidRole(role); } public boolean login(String username, String password) { diff --git a/web/src/main/java/org/bkvm/auth/UserRole.java b/web/src/main/java/org/bkvm/auth/UserRole.java index b9db9fb3..7dc141b0 100644 --- a/web/src/main/java/org/bkvm/auth/UserRole.java +++ b/web/src/main/java/org/bkvm/auth/UserRole.java @@ -19,10 +19,15 @@ */ package org.bkvm.auth; -import lombok.experimental.FieldNameConstants; +import java.util.Set; -@FieldNameConstants -public enum UserRole { - @FieldNameConstants.Include Admin, - @FieldNameConstants.Include User +public class UserRole { + public static class Fields { + public static final String Admin = "Admin"; + public static final String User = "User"; + private static final Set ALL = Set.of(Admin, User); + public static boolean isValidRole(String role) { + return ALL.contains(role); + } + } } diff --git a/web/src/main/java/org/bkvm/bookkeeper/BookkeeperClusterPool.java b/web/src/main/java/org/bkvm/bookkeeper/BookkeeperClusterPool.java index a9f9ca2b..ba810c68 100644 --- a/web/src/main/java/org/bkvm/bookkeeper/BookkeeperClusterPool.java +++ b/web/src/main/java/org/bkvm/bookkeeper/BookkeeperClusterPool.java @@ -65,7 +65,7 @@ public BookkeeperCluster ensureCluster(int clusterId, String metadataServiceUri, conf.setProperty(p, properties.get(p)); } } catch (IOException ex) { - LOG.log(Level.INFO, "Wrong configuration passed {0}", configuration); + LOG.log(Level.SEVERE, "Wrong configuration passed {0}", configuration); } LOG.log(Level.INFO, "Creating cluster {0}, at {1}", new Object[]{clusterId, metadataServiceUri}); diff --git a/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java b/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java index b4911286..8331ee65 100644 --- a/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java +++ b/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java @@ -283,12 +283,11 @@ public void doRefreshMetadataCache() { } Iterable ledgersIds = bkAdmin.listLedgers(); + metadataCache.clearLedgers(clusterId); for (long ledgerId : ledgersIds) { LedgerMetadata ledgerMetadata = readLedgerMetadata(ledgerId, clusterId); if (ledgerMetadata == null) { - // ledger disappeared - metadataCache.deleteLedger(clusterId, ledgerId); - return; + continue; } Ledger ledger = new Ledger(ledgerId, clusterId, ledgerMetadata.getLength(), @@ -306,7 +305,7 @@ public void doRefreshMetadataCache() { bookies.add(new LedgerBookie(ledgerId, bookieId, clusterId)); }); LOG.log(Level.FINE, "Updating ledger {0} metadata", ledgerId); - metadataCache.updateLedger(ledger, bookies, metadataEntries); + metadataCache.insertLedger(ledger, bookies, metadataEntries); } } topologyCache.refreshBookiesTopology(); diff --git a/web/src/main/java/org/bkvm/cache/MetadataCache.java b/web/src/main/java/org/bkvm/cache/MetadataCache.java index 2026d99b..224be30b 100644 --- a/web/src/main/java/org/bkvm/cache/MetadataCache.java +++ b/web/src/main/java/org/bkvm/cache/MetadataCache.java @@ -207,13 +207,12 @@ public List listLedgers() { } } - public void updateLedger(Ledger ledger, List bookies, + public void insertLedger(Ledger ledger, List bookies, List metadataEntries) { try (EntityManagerWrapper emw = getEntityManager()) { EntityManager em = emw.em; em.getTransaction().begin(); long ledgerId = ledger.getLedgerId(); - innerDeleteLedger(ledger.getClusterId(), ledger.getLedgerId(), em); em.persist(ledger); bookies.forEach((lb) -> { if (ledgerId != lb.getLedgerId()) { @@ -234,21 +233,17 @@ public void updateLedger(Ledger ledger, List bookies, } } - public void deleteLedger(int clusterId, long ledgerId) { + public void clearLedgers(int clusterId) { try (EntityManagerWrapper emw = getEntityManager()) { EntityManager em = emw.em; em.getTransaction().begin(); - innerDeleteLedger(clusterId, ledgerId, em); + em.createQuery("DELETE FROM ledger_metadata lm where lm.clusterId=" + clusterId).executeUpdate(); + em.createQuery("DELETE FROM ledger_bookie lm where lm.clusterId=" + clusterId).executeUpdate(); + em.createQuery("DELETE FROM ledger lm where lm.clusterId=" + clusterId).executeUpdate(); em.getTransaction().commit(); } } - private void innerDeleteLedger(int clusterId, long ledgerId, EntityManager em) { - em.createQuery("DELETE FROM ledger_metadata lm where lm.ledgerId=" + ledgerId + " and lm.clusterId=" + clusterId).executeUpdate(); - em.createQuery("DELETE FROM ledger_bookie lm where lm.ledgerId=" + ledgerId + " and lm.clusterId=" + clusterId).executeUpdate(); - em.createQuery("DELETE FROM ledger lm where lm.ledgerId=" + ledgerId + " and lm.clusterId=" + clusterId).executeUpdate(); - } - public Ledger getLedgerMetadata(int clusterId, long ledgerId) { try (EntityManagerWrapper emw = getEntityManager()) { EntityManager em = emw.em; diff --git a/web/src/test/java/org/bkvm/cache/LoadMetadataCacheTest.java b/web/src/test/java/org/bkvm/cache/LoadMetadataCacheTest.java new file mode 100644 index 00000000..d9b0d725 --- /dev/null +++ b/web/src/test/java/org/bkvm/cache/LoadMetadataCacheTest.java @@ -0,0 +1,64 @@ +/* + Licensed to Diennea S.r.l. under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. Diennea S.r.l. licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + + */ +package org.bkvm.cache; + +import org.apache.bookkeeper.client.BookKeeper; +import org.apache.bookkeeper.client.api.WriteHandle; +import org.apache.bookkeeper.conf.ClientConfiguration; +import org.bkvm.bookkeeper.BookkeeperManager; +import org.bkvm.utils.BookkeeperManagerTestUtils; +import org.junit.Test; + +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class LoadMetadataCacheTest extends BookkeeperManagerTestUtils { + + @Test + public void testLoad() throws Exception { + startBookie(false, -1); + ClientConfiguration bkConf = new ClientConfiguration(); + bkConf.setMetadataServiceUri(getMetadataServiceUri()); + + BookKeeper bk = BookKeeper.forConfig(bkConf).build(); + WriteHandle wr = bk.newCreateLedgerOp() + .withAckQuorumSize(1) + .withEnsembleSize(1) + .withWriteQuorumSize(1) + .withPassword("p".getBytes()) + .withCustomMetadata(Map.of("meta1", "value1".getBytes())) + .execute().get(); + wr.append("test".getBytes()); + final BookkeeperManager bookkeeperManager = getBookkeeperManager(); + bookkeeperManager.doRefreshMetadataCache(); + assertEquals(1, bookkeeperManager.getAllLedgers().size()); + + bk.newDeleteLedgerOp() + .withLedgerId(wr.getId()) + .execute().get(); + bookkeeperManager.doRefreshMetadataCache(); + assertEquals(0, bookkeeperManager.getAllLedgers().size()); + } + +} \ No newline at end of file diff --git a/web/src/test/java/org/bkvm/cache/MetadataCacheTest.java b/web/src/test/java/org/bkvm/cache/MetadataCacheTest.java index f578055e..d3258abc 100644 --- a/web/src/test/java/org/bkvm/cache/MetadataCacheTest.java +++ b/web/src/test/java/org/bkvm/cache/MetadataCacheTest.java @@ -48,7 +48,7 @@ public void test() { entries.add(new LedgerMetadataEntry(1, clusterId, "other", "foo")); entries.add(new LedgerMetadataEntry(1, clusterId, "metadataentry", "4933")); entries.add(new LedgerMetadataEntry(1, clusterId, "metadataentry2", "Thu Aug 22 2019 12:29:58 GMT+0200 (Central European Summer Time)")); - metadataCache.updateLedger(ledger, lb, entries); + metadataCache.insertLedger(ledger, lb, entries); List ledgers = metadataCache.listLedgers(); assertEquals(1, ledgers.size()); assertEquals(1024, ledgers.get(0).getSize()); @@ -87,7 +87,7 @@ public void test() { // UPDATE, just the size Ledger ledger2 = new Ledger(1, clusterId, 2048, new java.sql.Timestamp(System.currentTimeMillis()), new java.sql.Timestamp(System.currentTimeMillis()), ""); - metadataCache.updateLedger(ledger2, lb, entries); + metadataCache.insertLedger(ledger2, lb, entries); List ledgers2 = metadataCache.listLedgers(); assertEquals(1, ledgers2.size()); assertEquals(2048, ledgers2.get(0).getSize()); @@ -105,7 +105,7 @@ public void test() { List lb2 = new ArrayList<>(); lb2.add(new LedgerBookie(1, "localhost:1234", clusterId)); lb2.add(new LedgerBookie(1, "localhost:1236", clusterId)); - metadataCache.updateLedger(ledger2rewritten, lb2, entries); + metadataCache.insertLedger(ledger2rewritten, lb2, entries); assertEquals(1, metadataCache.getLedgersForBookie(clusterId, "localhost:1234").size()); assertEquals(0, metadataCache.getLedgersForBookie(clusterId, "localhost:1235").size()); diff --git a/web/src/test/java/org/bkvm/utils/BookkeeperManagerTestUtils.java b/web/src/test/java/org/bkvm/utils/BookkeeperManagerTestUtils.java index 59e06560..62c85f9c 100644 --- a/web/src/test/java/org/bkvm/utils/BookkeeperManagerTestUtils.java +++ b/web/src/test/java/org/bkvm/utils/BookkeeperManagerTestUtils.java @@ -28,6 +28,7 @@ import org.bkvm.config.ConfigurationStore; import org.bkvm.config.PropertiesConfigurationStore; import org.bkvm.config.ServerConfiguration; +import org.jetbrains.annotations.NotNull; import org.junit.After; import org.junit.Before; @@ -55,17 +56,17 @@ public void beforeSetup() throws Exception { datasource = new HerdDBEmbeddedDataSource(); datasource.setUrl("jdbc:herddb:local"); metadataCache = new MetadataCache(datasource); - final Properties properties = new Properties(); - properties.put(ServerConfiguration.PROPERTY_BOOKKEEPER_METADATA_SERVICE_URI, - "zk+null://" + getZooKeeperAddress() + "/ledgers"); - ConfigurationStore config = new PropertiesConfigurationStore(new Properties()); bookkeeperManager = new BookkeeperManager(config, metadataCache); init(); } protected void init() throws Exception { - createCluster("cluster1", "zk+null://" + getZooKeeperAddress() + "/ledgers", ""); + createCluster("cluster1", getMetadataServiceUri(), ""); + } + + protected String getMetadataServiceUri() { + return "zk+null://" + getZooKeeperAddress() + "/ledgers"; } protected Cluster createCluster(String name, String metadataServiceUri, String configuration) throws BookkeeperManagerException { From 11b7424725b7a8a7fd31ce9b199b6b06d3c0b147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Boschi?= Date: Tue, 30 Apr 2024 12:24:22 +0200 Subject: [PATCH 2/4] fix --- pom.xml | 6 ++-- .../main/java/org/bkvm/auth/AuthManager.java | 1 - .../bkvm/bookkeeper/BookkeeperManager.java | 15 ++++++--- .../java/org/bkvm/cache/MetadataCache.java | 21 +++++++++--- .../org/bkvm/cache/LoadMetadataCacheTest.java | 33 ++++++++++--------- .../utils/BookkeeperManagerTestUtils.java | 2 -- 6 files changed, 49 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index aeef1374..31e60d57 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ 2.35 3.1.1 4.1.4 - 2.7.5 + 2.7.14 3.1.7 2.3.1 @@ -70,7 +70,7 @@ 6.1.6 1.0.2.3 33.1.0-jre - 3.19.6 + 3.24.0 1.6.10 6.5.0 @@ -107,7 +107,7 @@ org.eclipse.persistence org.eclipse.persistence.asm - ${eclipselink.version} + 9.7.0 org.eclipse.persistence diff --git a/web/src/main/java/org/bkvm/auth/AuthManager.java b/web/src/main/java/org/bkvm/auth/AuthManager.java index 4cc5825a..fd4898ad 100644 --- a/web/src/main/java/org/bkvm/auth/AuthManager.java +++ b/web/src/main/java/org/bkvm/auth/AuthManager.java @@ -23,7 +23,6 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import org.apache.commons.lang3.EnumUtils; import org.bkvm.config.ConfigurationStore; /** diff --git a/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java b/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java index 8331ee65..5261c3ef 100644 --- a/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java +++ b/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java @@ -129,6 +129,7 @@ public enum RefreshStatus { IDLE, WORKING } + private volatile long lastMetadataCacheRefresh; private final ConcurrentHashMap lastClusterWideConfiguration = new ConcurrentHashMap<>(); private final AtomicReference refreshStatus = new AtomicReference<>(RefreshStatus.IDLE); @@ -167,7 +168,7 @@ public static final class RefreshCacheWorkerStatus { private final Map lastClusterWideConfiguration; public RefreshCacheWorkerStatus(RefreshStatus status, long lastMetadataCacheRefresh, - Map lastClusterWideConfiguration) { + Map lastClusterWideConfiguration) { this.status = status; this.lastMetadataCacheRefresh = lastMetadataCacheRefresh; this.lastClusterWideConfiguration = lastClusterWideConfiguration; @@ -281,14 +282,15 @@ public void doRefreshMetadataCache() { metadataCache.deleteBookie(b.getClusterId(), b.getBookieId()); } } + Set deletedLedgers = new HashSet<>(metadataCache.listLedgers(clusterId)); Iterable ledgersIds = bkAdmin.listLedgers(); - metadataCache.clearLedgers(clusterId); for (long ledgerId : ledgersIds) { LedgerMetadata ledgerMetadata = readLedgerMetadata(ledgerId, clusterId); if (ledgerMetadata == null) { continue; } + deletedLedgers.remove(ledgerId); Ledger ledger = new Ledger(ledgerId, clusterId, ledgerMetadata.getLength(), new java.sql.Timestamp(ledgerMetadata.getCtime()), @@ -307,6 +309,10 @@ public void doRefreshMetadataCache() { LOG.log(Level.FINE, "Updating ledger {0} metadata", ledgerId); metadataCache.insertLedger(ledger, bookies, metadataEntries); } + for (Long deletedLedger : deletedLedgers) { + metadataCache.deleteLedger(clusterId, deletedLedger); + } + } topologyCache.refreshBookiesTopology(); @@ -527,7 +533,8 @@ private static ClusterWideConfiguration getClusterWideConfiguration(int clusterI autoRecoveryEnabled = underreplicationManager.isLedgerReplicationEnabled(); lostBookieRecoveryDelay = underreplicationManager.getLostBookieRecoveryDelay(); } - } catch (ReplicationException.UnavailableException | ReplicationException.CompatibilityException notConfigured) { + } catch (ReplicationException.UnavailableException + | ReplicationException.CompatibilityException notConfigured) { // auto replication stuff never initialized LOG.log(Level.INFO, "Cannot get auditor info: {0}", notConfigured + ""); // do not write stacktrace } @@ -541,7 +548,7 @@ private static ClusterWideConfiguration getClusterWideConfiguration(int clusterI autoRecoveryEnabled, lostBookieRecoveryDelay, layoutFormatVersion, layoutManagerFactoryClass, layoutManagerVersion); } catch (InterruptedException - | MetadataException | IOException ex) { + | MetadataException | IOException ex) { LOG.log(Level.SEVERE, "Error", ex); throw new BookkeeperManagerException(ex); } finally { diff --git a/web/src/main/java/org/bkvm/cache/MetadataCache.java b/web/src/main/java/org/bkvm/cache/MetadataCache.java index 224be30b..bc375fa8 100644 --- a/web/src/main/java/org/bkvm/cache/MetadataCache.java +++ b/web/src/main/java/org/bkvm/cache/MetadataCache.java @@ -207,11 +207,20 @@ public List listLedgers() { } } + public List listLedgers(int clusterId) { + try (EntityManagerWrapper emw = getEntityManager()) { + EntityManager em = emw.em; + Query q = em.createQuery("select l.ledgerId from ledger l where l.clusterId=" + clusterId, Ledger.class); + return q.getResultList(); + } + } + public void insertLedger(Ledger ledger, List bookies, List metadataEntries) { try (EntityManagerWrapper emw = getEntityManager()) { EntityManager em = emw.em; em.getTransaction().begin(); + innerDeleteLedger(ledger.getClusterId(), ledger.getLedgerId(), em); long ledgerId = ledger.getLedgerId(); em.persist(ledger); bookies.forEach((lb) -> { @@ -233,17 +242,21 @@ public void insertLedger(Ledger ledger, List bookies, } } - public void clearLedgers(int clusterId) { + public void deleteLedger(int clusterId, long ledgerId) { try (EntityManagerWrapper emw = getEntityManager()) { EntityManager em = emw.em; em.getTransaction().begin(); - em.createQuery("DELETE FROM ledger_metadata lm where lm.clusterId=" + clusterId).executeUpdate(); - em.createQuery("DELETE FROM ledger_bookie lm where lm.clusterId=" + clusterId).executeUpdate(); - em.createQuery("DELETE FROM ledger lm where lm.clusterId=" + clusterId).executeUpdate(); + innerDeleteLedger(clusterId, ledgerId, em); em.getTransaction().commit(); } } + private static void innerDeleteLedger(int clusterId, long ledgerId, EntityManager em) { + em.createQuery("DELETE FROM ledger_metadata lm where lm.ledgerId=" + ledgerId + " and lm.clusterId=" + clusterId).executeUpdate(); + em.createQuery("DELETE FROM ledger_bookie lm where lm.ledgerId=" + ledgerId + " and lm.clusterId=" + clusterId).executeUpdate(); + em.createQuery("DELETE FROM ledger lm where lm.ledgerId=" + ledgerId + " and lm.clusterId=" + clusterId).executeUpdate(); + } + public Ledger getLedgerMetadata(int clusterId, long ledgerId) { try (EntityManagerWrapper emw = getEntityManager()) { EntityManager em = emw.em; diff --git a/web/src/test/java/org/bkvm/cache/LoadMetadataCacheTest.java b/web/src/test/java/org/bkvm/cache/LoadMetadataCacheTest.java index d9b0d725..839852f0 100644 --- a/web/src/test/java/org/bkvm/cache/LoadMetadataCacheTest.java +++ b/web/src/test/java/org/bkvm/cache/LoadMetadataCacheTest.java @@ -19,6 +19,9 @@ */ package org.bkvm.cache; +import static org.junit.Assert.assertEquals; +import java.util.Map; +import java.util.concurrent.ExecutionException; import org.apache.bookkeeper.client.BookKeeper; import org.apache.bookkeeper.client.api.WriteHandle; import org.apache.bookkeeper.conf.ClientConfiguration; @@ -26,12 +29,6 @@ import org.bkvm.utils.BookkeeperManagerTestUtils; import org.junit.Test; -import java.util.Collection; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; public class LoadMetadataCacheTest extends BookkeeperManagerTestUtils { @@ -42,23 +39,29 @@ public void testLoad() throws Exception { bkConf.setMetadataServiceUri(getMetadataServiceUri()); BookKeeper bk = BookKeeper.forConfig(bkConf).build(); - WriteHandle wr = bk.newCreateLedgerOp() - .withAckQuorumSize(1) - .withEnsembleSize(1) - .withWriteQuorumSize(1) - .withPassword("p".getBytes()) - .withCustomMetadata(Map.of("meta1", "value1".getBytes())) - .execute().get(); + WriteHandle wr0 = createLedger(bk); + WriteHandle wr = createLedger(bk); wr.append("test".getBytes()); final BookkeeperManager bookkeeperManager = getBookkeeperManager(); bookkeeperManager.doRefreshMetadataCache(); - assertEquals(1, bookkeeperManager.getAllLedgers().size()); + assertEquals(2, bookkeeperManager.getAllLedgers().size()); bk.newDeleteLedgerOp() .withLedgerId(wr.getId()) .execute().get(); bookkeeperManager.doRefreshMetadataCache(); - assertEquals(0, bookkeeperManager.getAllLedgers().size()); + assertEquals(1, bookkeeperManager.getAllLedgers().size()); + } + + private static WriteHandle createLedger(BookKeeper bk) throws InterruptedException, ExecutionException { + WriteHandle wr = bk.newCreateLedgerOp() + .withAckQuorumSize(1) + .withEnsembleSize(1) + .withWriteQuorumSize(1) + .withPassword("p".getBytes()) + .withCustomMetadata(Map.of("meta1", "value1".getBytes())) + .execute().get(); + return wr; } } \ No newline at end of file diff --git a/web/src/test/java/org/bkvm/utils/BookkeeperManagerTestUtils.java b/web/src/test/java/org/bkvm/utils/BookkeeperManagerTestUtils.java index 62c85f9c..37a17f35 100644 --- a/web/src/test/java/org/bkvm/utils/BookkeeperManagerTestUtils.java +++ b/web/src/test/java/org/bkvm/utils/BookkeeperManagerTestUtils.java @@ -27,8 +27,6 @@ import org.bkvm.cache.MetadataCache; import org.bkvm.config.ConfigurationStore; import org.bkvm.config.PropertiesConfigurationStore; -import org.bkvm.config.ServerConfiguration; -import org.jetbrains.annotations.NotNull; import org.junit.After; import org.junit.Before; From 99bf22cfee1b599b8e16cf23f46e8d33acd59373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Boschi?= Date: Tue, 30 Apr 2024 12:27:30 +0200 Subject: [PATCH 3/4] fix --- .../main/java/org/bkvm/bookkeeper/BookkeeperManager.java | 2 +- web/src/main/java/org/bkvm/cache/MetadataCache.java | 4 ++-- web/src/test/java/org/bkvm/cache/MetadataCacheTest.java | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java b/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java index 5261c3ef..31b46a43 100644 --- a/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java +++ b/web/src/main/java/org/bkvm/bookkeeper/BookkeeperManager.java @@ -307,7 +307,7 @@ public void doRefreshMetadataCache() { bookies.add(new LedgerBookie(ledgerId, bookieId, clusterId)); }); LOG.log(Level.FINE, "Updating ledger {0} metadata", ledgerId); - metadataCache.insertLedger(ledger, bookies, metadataEntries); + metadataCache.updateLedger(ledger, bookies, metadataEntries); } for (Long deletedLedger : deletedLedgers) { metadataCache.deleteLedger(clusterId, deletedLedger); diff --git a/web/src/main/java/org/bkvm/cache/MetadataCache.java b/web/src/main/java/org/bkvm/cache/MetadataCache.java index bc375fa8..0479a58b 100644 --- a/web/src/main/java/org/bkvm/cache/MetadataCache.java +++ b/web/src/main/java/org/bkvm/cache/MetadataCache.java @@ -215,13 +215,13 @@ public List listLedgers(int clusterId) { } } - public void insertLedger(Ledger ledger, List bookies, + public void updateLedger(Ledger ledger, List bookies, List metadataEntries) { try (EntityManagerWrapper emw = getEntityManager()) { EntityManager em = emw.em; em.getTransaction().begin(); - innerDeleteLedger(ledger.getClusterId(), ledger.getLedgerId(), em); long ledgerId = ledger.getLedgerId(); + innerDeleteLedger(ledger.getClusterId(), ledgerId, em); em.persist(ledger); bookies.forEach((lb) -> { if (ledgerId != lb.getLedgerId()) { diff --git a/web/src/test/java/org/bkvm/cache/MetadataCacheTest.java b/web/src/test/java/org/bkvm/cache/MetadataCacheTest.java index d3258abc..f578055e 100644 --- a/web/src/test/java/org/bkvm/cache/MetadataCacheTest.java +++ b/web/src/test/java/org/bkvm/cache/MetadataCacheTest.java @@ -48,7 +48,7 @@ public void test() { entries.add(new LedgerMetadataEntry(1, clusterId, "other", "foo")); entries.add(new LedgerMetadataEntry(1, clusterId, "metadataentry", "4933")); entries.add(new LedgerMetadataEntry(1, clusterId, "metadataentry2", "Thu Aug 22 2019 12:29:58 GMT+0200 (Central European Summer Time)")); - metadataCache.insertLedger(ledger, lb, entries); + metadataCache.updateLedger(ledger, lb, entries); List ledgers = metadataCache.listLedgers(); assertEquals(1, ledgers.size()); assertEquals(1024, ledgers.get(0).getSize()); @@ -87,7 +87,7 @@ public void test() { // UPDATE, just the size Ledger ledger2 = new Ledger(1, clusterId, 2048, new java.sql.Timestamp(System.currentTimeMillis()), new java.sql.Timestamp(System.currentTimeMillis()), ""); - metadataCache.insertLedger(ledger2, lb, entries); + metadataCache.updateLedger(ledger2, lb, entries); List ledgers2 = metadataCache.listLedgers(); assertEquals(1, ledgers2.size()); assertEquals(2048, ledgers2.get(0).getSize()); @@ -105,7 +105,7 @@ public void test() { List lb2 = new ArrayList<>(); lb2.add(new LedgerBookie(1, "localhost:1234", clusterId)); lb2.add(new LedgerBookie(1, "localhost:1236", clusterId)); - metadataCache.insertLedger(ledger2rewritten, lb2, entries); + metadataCache.updateLedger(ledger2rewritten, lb2, entries); assertEquals(1, metadataCache.getLedgersForBookie(clusterId, "localhost:1234").size()); assertEquals(0, metadataCache.getLedgersForBookie(clusterId, "localhost:1235").size()); From f81918ceda1403d6be6ad05478d679edc46c582f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Boschi?= Date: Tue, 30 Apr 2024 13:23:31 +0200 Subject: [PATCH 4/4] add docker compose --- docker/Dockerfile | 1 - docker/docker-compose.yaml | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 docker/docker-compose.yaml diff --git a/docker/Dockerfile b/docker/Dockerfile index f4d90028..ad2cf413 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -7,7 +7,6 @@ RUN test -n "$TARBALL" ENV WEB_PORT=4500 EXPOSE $WEB_PORT -ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64 RUN apt-get update \ && apt-get -y dist-upgrade \ diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml new file mode 100644 index 00000000..af61e5b2 --- /dev/null +++ b/docker/docker-compose.yaml @@ -0,0 +1,48 @@ +version: '3' +services: + zk: + image: zookeeper:3.9.2 + hostname: zk + ports: + - "2181:2181" + healthcheck: + test: ["CMD", "curl", "-sf", "http://localhost:8080/commands/stat"] + interval: 5s + timeout: 5s + retries: 20 + restart: always + + bookie-1: + image: apache/bookkeeper:4.16.5 + hostname: bk-1 + ports: + - "3181:3181" + - "8080:8080" + environment: + - BK_zkServers=zk:2181 + - BK_metadataServiceUri=zk+null://zk:2181/ledgers + - BK_DATA_DIR=/data/bookkeeper + - BK_useHostNameAsBookieID=true + - BK_bookiePort=3181 + - BK_httpServerEnabled=true + depends_on: + - "zk" + restart: always + + bookie-1-insert: + image: apache/bookkeeper:4.16.5 + depends_on: + - bookie-1 + restart: on-failure + environment: + - BK_zkServers=zk:2181 + - BK_metadataServiceUri=zk+null://zk:2181/ledgers + entrypoint: [ "bash", "-c", "source /opt/bookkeeper/scripts/common.sh && bin/bookkeeper shell simpletest -e 1 -a 1 -w 1" ] + + bkvm: + ports: + - "4500:4500" + image: bkvm/bkvm:latest + environment: + BKVM_metadataServiceUri: zk://zk:2181/ledgers +