Search in sources :

Example 1 with Notification

use of org.apache.pulsar.metadata.api.Notification in project pulsar by apache.

the class ZKMetadataStore method handleWatchEvent.

private void handleWatchEvent(WatchedEvent event) {
    if (log.isDebugEnabled()) {
        log.debug("Received ZK watch : {}", event);
    }
    String path = event.getPath();
    if (path == null) {
        // Ignore Session events
        return;
    }
    String parent = parent(path);
    Notification childrenChangedNotification = null;
    NotificationType type;
    switch(event.getType()) {
        case NodeCreated:
            type = NotificationType.Created;
            if (parent != null) {
                childrenChangedNotification = new Notification(NotificationType.ChildrenChanged, parent);
            }
            break;
        case NodeDataChanged:
            type = NotificationType.Modified;
            break;
        case NodeChildrenChanged:
            type = NotificationType.ChildrenChanged;
            break;
        case NodeDeleted:
            type = NotificationType.Deleted;
            if (parent != null) {
                childrenChangedNotification = new Notification(NotificationType.ChildrenChanged, parent);
            }
            break;
        default:
            return;
    }
    receivedNotification(new Notification(type, event.getPath()));
    if (childrenChangedNotification != null) {
        receivedNotification(childrenChangedNotification);
    }
}
Also used : NotificationType(org.apache.pulsar.metadata.api.NotificationType) Notification(org.apache.pulsar.metadata.api.Notification)

Example 2 with Notification

use of org.apache.pulsar.metadata.api.Notification in project pulsar by apache.

the class AbstractMetadataStore method notifyParentChildrenChanged.

protected void notifyParentChildrenChanged(String path) {
    String parent = parent(path);
    while (parent != null) {
        receivedNotification(new Notification(NotificationType.ChildrenChanged, parent));
        parent = parent(parent);
    }
}
Also used : Notification(org.apache.pulsar.metadata.api.Notification)

Example 3 with Notification

use of org.apache.pulsar.metadata.api.Notification in project pulsar by yahoo.

the class ModularLoadManagerImplTest method testLoadShedding.

// Test that load shedding works
@Test
public void testLoadShedding() throws Exception {
    final NamespaceBundleStats stats1 = new NamespaceBundleStats();
    final NamespaceBundleStats stats2 = new NamespaceBundleStats();
    stats1.msgRateIn = 100;
    stats2.msgRateIn = 200;
    final Map<String, NamespaceBundleStats> statsMap = new ConcurrentHashMap<>();
    statsMap.put(mockBundleName(1), stats1);
    statsMap.put(mockBundleName(2), stats2);
    final LocalBrokerData localBrokerData = new LocalBrokerData();
    localBrokerData.update(new SystemResourceUsage(), statsMap);
    final Namespaces namespacesSpy1 = spy(pulsar1.getAdminClient().namespaces());
    AtomicReference<String> bundleReference = new AtomicReference<>();
    doAnswer(invocation -> {
        bundleReference.set(invocation.getArguments()[0].toString() + '/' + invocation.getArguments()[1]);
        return null;
    }).when(namespacesSpy1).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    setField(pulsar1.getAdminClient(), "namespaces", namespacesSpy1);
    pulsar1.getConfiguration().setLoadBalancerEnabled(true);
    final LoadData loadData = (LoadData) getField(primaryLoadManager, "loadData");
    final Map<String, BrokerData> brokerDataMap = loadData.getBrokerData();
    final BrokerData brokerDataSpy1 = spy(brokerDataMap.get(primaryHost));
    when(brokerDataSpy1.getLocalData()).thenReturn(localBrokerData);
    brokerDataMap.put(primaryHost, brokerDataSpy1);
    // Need to update all the bundle data for the shredder to see the spy.
    primaryLoadManager.handleDataNotification(new Notification(NotificationType.Created, LoadManager.LOADBALANCE_BROKERS_ROOT + "/broker:8080"));
    Thread.sleep(100);
    localBrokerData.setCpu(new ResourceUsage(80, 100));
    primaryLoadManager.doLoadShedding();
    // 80% is below overload threshold: verify nothing is unloaded.
    verify(namespacesSpy1, Mockito.times(0)).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    localBrokerData.setCpu(new ResourceUsage(90, 100));
    primaryLoadManager.doLoadShedding();
    // Most expensive bundle will be unloaded.
    verify(namespacesSpy1, Mockito.times(1)).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    assertEquals(bundleReference.get(), mockBundleName(2));
    primaryLoadManager.doLoadShedding();
    // Now less expensive bundle will be unloaded (normally other bundle would move off and nothing would be
    // unloaded, but this is not the case due to the spy's behavior).
    verify(namespacesSpy1, Mockito.times(2)).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    assertEquals(bundleReference.get(), mockBundleName(1));
    primaryLoadManager.doLoadShedding();
    // Now both are in grace period: neither should be unloaded.
    verify(namespacesSpy1, Mockito.times(2)).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
}
Also used : Namespaces(org.apache.pulsar.client.admin.Namespaces) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) BrokerData(org.apache.pulsar.policies.data.loadbalancer.BrokerData) TimeAverageBrokerData(org.apache.pulsar.policies.data.loadbalancer.TimeAverageBrokerData) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) ResourceUsage(org.apache.pulsar.policies.data.loadbalancer.ResourceUsage) SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) AtomicReference(java.util.concurrent.atomic.AtomicReference) Notification(org.apache.pulsar.metadata.api.Notification) NamespaceBundleStats(org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test)

Example 4 with Notification

use of org.apache.pulsar.metadata.api.Notification in project pulsar by yahoo.

the class MetadataStoreTest method notificationListeners.

@Test(dataProvider = "impl")
public void notificationListeners(String provider, Supplier<String> urlSupplier) throws Exception {
    @Cleanup MetadataStore store = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
    BlockingQueue<Notification> notifications = new LinkedBlockingDeque<>();
    store.registerListener(n -> {
        notifications.add(n);
    });
    String key1 = newKey();
    assertFalse(store.get(key1).join().isPresent());
    // Trigger created notification
    Stat stat = store.put(key1, "value-1".getBytes(), Optional.empty()).join();
    assertTrue(store.get(key1).join().isPresent());
    assertEquals(store.getChildren(key1).join(), Collections.emptyList());
    assertEquals(stat.getVersion(), 0);
    Notification n = notifications.poll(3, TimeUnit.SECONDS);
    assertNotNull(n);
    assertEquals(n.getType(), NotificationType.Created);
    assertEquals(n.getPath(), key1);
    // Trigger modified notification
    stat = store.put(key1, "value-2".getBytes(), Optional.empty()).join();
    n = notifications.poll(3, TimeUnit.SECONDS);
    assertNotNull(n);
    assertEquals(n.getType(), NotificationType.Modified);
    assertEquals(n.getPath(), key1);
    assertEquals(stat.getVersion(), 1);
    // Trigger modified notification on the parent
    String key1Child = key1 + "/xx";
    assertFalse(store.get(key1Child).join().isPresent());
    store.put(key1Child, "value-2".getBytes(), Optional.empty()).join();
    n = notifications.poll(3, TimeUnit.SECONDS);
    assertNotNull(n);
    assertEquals(n.getType(), NotificationType.Created);
    assertEquals(n.getPath(), key1Child);
    n = notifications.poll(3, TimeUnit.SECONDS);
    assertNotNull(n);
    assertEquals(n.getType(), NotificationType.ChildrenChanged);
    assertEquals(n.getPath(), key1);
    assertTrue(store.exists(key1Child).join());
    assertEquals(store.getChildren(key1).join(), Collections.singletonList("xx"));
    store.delete(key1Child, Optional.empty()).join();
    n = notifications.poll(3, TimeUnit.SECONDS);
    assertNotNull(n);
    assertEquals(n.getType(), NotificationType.Deleted);
    assertEquals(n.getPath(), key1Child);
    // Parent should be notified of the deletion
    n = notifications.poll(3, TimeUnit.SECONDS);
    assertNotNull(n);
    assertEquals(n.getType(), NotificationType.ChildrenChanged);
    assertEquals(n.getPath(), key1);
}
Also used : MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Stat(org.apache.pulsar.metadata.api.Stat) Cleanup(lombok.Cleanup) Notification(org.apache.pulsar.metadata.api.Notification) Test(org.testng.annotations.Test)

Example 5 with Notification

use of org.apache.pulsar.metadata.api.Notification in project pulsar by yahoo.

the class RocksdbMetadataStore method storeDelete.

@Override
protected CompletableFuture<Void> storeDelete(String path, Optional<Long> expectedVersion) {
    if (log.isDebugEnabled()) {
        log.debug("storeDelete.path={},instanceId={}", path, instanceId);
    }
    try {
        dbStateLock.readLock().lock();
        if (state == State.CLOSED) {
            throw new MetadataStoreException.AlreadyClosedException("");
        }
        try (Transaction transaction = db.beginTransaction(optionSync)) {
            byte[] pathBytes = toBytes(path);
            byte[] oldValueData = transaction.getForUpdate(optionDontCache, pathBytes, false);
            MetaValue metaValue = MetaValue.parse(oldValueData);
            if (metaValue == null) {
                throw new MetadataStoreException.NotFoundException(String.format("path %s not found.", path));
            }
            if (expectedVersion.isPresent() && !expectedVersion.get().equals(metaValue.getVersion())) {
                throw new MetadataStoreException.BadVersionException(String.format("Version mismatch, actual=%s, expect=%s", metaValue.getVersion(), expectedVersion.get()));
            }
            transaction.delete(pathBytes);
            transaction.commit();
            receivedNotification(new Notification(NotificationType.Deleted, path));
            notifyParentChildrenChanged(path);
            return CompletableFuture.completedFuture(null);
        }
    } catch (Throwable e) {
        return FutureUtil.failedFuture(MetadataStoreException.wrap(e));
    } finally {
        dbStateLock.readLock().unlock();
    }
}
Also used : Transaction(org.rocksdb.Transaction) Notification(org.apache.pulsar.metadata.api.Notification)

Aggregations

Notification (org.apache.pulsar.metadata.api.Notification)25 Stat (org.apache.pulsar.metadata.api.Stat)9 NotificationType (org.apache.pulsar.metadata.api.NotificationType)6 Transaction (org.rocksdb.Transaction)6 Test (org.testng.annotations.Test)6 MetadataStoreException (org.apache.pulsar.metadata.api.MetadataStoreException)4 BadVersionException (org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Cleanup (lombok.Cleanup)3 Namespaces (org.apache.pulsar.client.admin.Namespaces)3 MetadataStore (org.apache.pulsar.metadata.api.MetadataStore)3 LocalBrokerData (org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData)3 NamespaceBundleStats (org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats)3 ResourceUsage (org.apache.pulsar.policies.data.loadbalancer.ResourceUsage)3 SystemResourceUsage (org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 BrokerData (org.apache.pulsar.policies.data.loadbalancer.BrokerData)2 TimeAverageBrokerData (org.apache.pulsar.policies.data.loadbalancer.TimeAverageBrokerData)2