forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PIP-45: Added session events to metadata store (apache#9273)
* PIP-45: Added session events to metadata store * Added missing license header * Fixed session timeout in MockZookeeper * Increased test timeouts * Increase session timeout in ZKSessionTest * Fixed merge issue
- Loading branch information
Showing
9 changed files
with
441 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
pulsar-metadata/src/main/java/org/apache/pulsar/metadata/api/extended/SessionEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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.apache.pulsar.metadata.api.extended; | ||
|
||
/** | ||
* An event regarding a session of MetadataStore | ||
*/ | ||
public enum SessionEvent { | ||
|
||
/** | ||
* The client is temporarily disconnected, although the session is still valid | ||
*/ | ||
ConnectionLost, | ||
|
||
/** | ||
* The client was able to successfully reconnect | ||
*/ | ||
Reconnected, | ||
|
||
/** | ||
* The session was lost, all the ephemeral keys created on the store within the current session might have been | ||
* already expired. | ||
*/ | ||
SessionLost, | ||
|
||
/** | ||
* The session was established | ||
*/ | ||
SessionReestablished, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/ZKSessionWatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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.apache.pulsar.metadata.impl; | ||
|
||
import io.netty.util.concurrent.DefaultThreadFactory; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Consumer; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.apache.pulsar.metadata.api.extended.SessionEvent; | ||
import org.apache.zookeeper.AsyncCallback.StatCallback; | ||
import org.apache.zookeeper.KeeperException; | ||
import org.apache.zookeeper.WatchedEvent; | ||
import org.apache.zookeeper.Watcher; | ||
import org.apache.zookeeper.ZooKeeper; | ||
|
||
/** | ||
* Monitor the ZK session state every few seconds and send notifications | ||
*/ | ||
@Slf4j | ||
public class ZKSessionWatcher implements AutoCloseable, Watcher { | ||
private final ZooKeeper zk; | ||
|
||
private SessionEvent currentStatus; | ||
private final Consumer<SessionEvent> sessionListener; | ||
|
||
// Maximum time to wait for ZK session to be re-connected to quorum (set to 5/6 of SessionTimeout) | ||
private final long monitorTimeoutMillis; | ||
|
||
// Interval at which we check the state of the zk session (set to 1/15 of SessionTimeout) | ||
private final long tickTimeMillis; | ||
|
||
private final ScheduledExecutorService scheduler; | ||
private final ScheduledFuture<?> task; | ||
|
||
private long disconnectedAt = 0; | ||
|
||
public ZKSessionWatcher(ZooKeeper zk, Consumer<SessionEvent> sessionListener) { | ||
this.zk = zk; | ||
this.monitorTimeoutMillis = zk.getSessionTimeout() * 5 / 6; | ||
this.tickTimeMillis = zk.getSessionTimeout() / 15; | ||
this.sessionListener = sessionListener; | ||
|
||
this.scheduler = Executors | ||
.newSingleThreadScheduledExecutor(new DefaultThreadFactory("metadata-store-zk-session-watcher")); | ||
this.task = scheduler.scheduleAtFixedRate(this::checkConnectionStatus, tickTimeMillis, tickTimeMillis, | ||
TimeUnit.MILLISECONDS); | ||
this.currentStatus = SessionEvent.SessionReestablished; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
task.cancel(true); | ||
scheduler.shutdownNow(); | ||
scheduler.awaitTermination(10, TimeUnit.SECONDS); | ||
} | ||
|
||
// task that runs every TICK_TIME to check zk connection | ||
private synchronized void checkConnectionStatus() { | ||
try { | ||
CompletableFuture<Watcher.Event.KeeperState> future = new CompletableFuture<>(); | ||
zk.exists("/", false, (StatCallback) (rc, path, ctx, stat) -> { | ||
switch (KeeperException.Code.get(rc)) { | ||
case CONNECTIONLOSS: | ||
future.complete(Watcher.Event.KeeperState.Disconnected); | ||
break; | ||
|
||
case SESSIONEXPIRED: | ||
future.complete(Watcher.Event.KeeperState.Expired); | ||
break; | ||
|
||
case OK: | ||
default: | ||
future.complete(Watcher.Event.KeeperState.SyncConnected); | ||
} | ||
}, null); | ||
|
||
Watcher.Event.KeeperState zkClientState; | ||
try { | ||
zkClientState = future.get(tickTimeMillis, TimeUnit.MILLISECONDS); | ||
} catch (TimeoutException e) { | ||
// Consider zk disconnection if zk operation takes more than TICK_TIME | ||
zkClientState = Watcher.Event.KeeperState.Disconnected; | ||
} | ||
|
||
checkState(zkClientState); | ||
} catch (RejectedExecutionException | InterruptedException e) { | ||
task.cancel(true); | ||
} catch (Throwable t) { | ||
log.warn("Error while checking ZK connection status", t); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void process(WatchedEvent event) { | ||
checkState(event.getState()); | ||
} | ||
|
||
private void checkState(Watcher.Event.KeeperState zkClientState) { | ||
switch (zkClientState) { | ||
case Expired: | ||
if (currentStatus != SessionEvent.SessionLost) { | ||
log.error("ZooKeeper session expired"); | ||
currentStatus = SessionEvent.SessionLost; | ||
sessionListener.accept(currentStatus); | ||
} | ||
break; | ||
|
||
case Disconnected: | ||
if (disconnectedAt == 0) { | ||
// this is the first disconnect event, we should monitor the time out from now, so we record the | ||
// time of disconnect | ||
disconnectedAt = System.nanoTime(); | ||
} | ||
|
||
long timeRemainingMillis = monitorTimeoutMillis | ||
- TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - disconnectedAt); | ||
if (timeRemainingMillis <= 0 && currentStatus != SessionEvent.SessionLost) { | ||
log.error("ZooKeeper session reconnection timeout. Notifying session is lost."); | ||
currentStatus = SessionEvent.SessionLost; | ||
sessionListener.accept(currentStatus); | ||
} else if (currentStatus != SessionEvent.SessionLost) { | ||
log.warn("ZooKeeper client is disconnected. Waiting to reconnect, time remaining = {} seconds", | ||
timeRemainingMillis / 1000.0); | ||
if (currentStatus == SessionEvent.SessionReestablished) { | ||
currentStatus = SessionEvent.ConnectionLost; | ||
sessionListener.accept(currentStatus); | ||
} | ||
} | ||
break; | ||
|
||
default: | ||
if (currentStatus != SessionEvent.SessionReestablished) { | ||
// since it reconnected to zoo keeper, we reset the disconnected time | ||
log.info("ZooKeeper client reconnection with server quorum"); | ||
disconnectedAt = 0; | ||
|
||
sessionListener.accept(SessionEvent.Reconnected); | ||
if (currentStatus == SessionEvent.SessionLost) { | ||
sessionListener.accept(SessionEvent.SessionReestablished); | ||
} | ||
currentStatus = SessionEvent.SessionReestablished; | ||
} | ||
break; | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
pulsar-metadata/src/test/java/org/apache/pulsar/metadata/MetadataStoreExtendedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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.apache.pulsar.metadata; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNotEquals; | ||
import static org.testng.Assert.assertNotNull; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Optional; | ||
|
||
import lombok.Cleanup; | ||
|
||
import org.apache.pulsar.metadata.api.MetadataStoreConfig; | ||
import org.apache.pulsar.metadata.api.Stat; | ||
import org.apache.pulsar.metadata.api.extended.CreateOption; | ||
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended; | ||
import org.testng.annotations.Test; | ||
|
||
public class MetadataStoreExtendedTest extends BaseMetadataStoreTest { | ||
|
||
@Test(dataProvider = "impl") | ||
public void sequentialKeys(String provider, String url) throws Exception { | ||
final String basePath = "/my/path"; | ||
|
||
@Cleanup | ||
MetadataStoreExtended store = MetadataStoreExtended.create(url, MetadataStoreConfig.builder().build()); | ||
|
||
Stat stat1 = store.put(basePath, "value-1".getBytes(), Optional.of(-1L), EnumSet.of(CreateOption.Sequential)) | ||
.join(); | ||
assertNotNull(stat1); | ||
assertEquals(stat1.getVersion(), 0L); | ||
assertNotEquals(stat1.getPath(), basePath); | ||
assertEquals(store.get(stat1.getPath()).join().get().getValue(), "value-1".getBytes()); | ||
String seq1 = stat1.getPath().replace(basePath, ""); | ||
long n1 = Long.parseLong(seq1); | ||
|
||
Stat stat2 = store.put(basePath, "value-2".getBytes(), Optional.of(-1L), EnumSet.of(CreateOption.Sequential)) | ||
.join(); | ||
assertNotNull(stat2); | ||
assertEquals(stat2.getVersion(), 0L); | ||
assertNotEquals(stat2.getPath(), basePath); | ||
assertNotEquals(stat2.getPath(), stat1.getPath()); | ||
assertEquals(store.get(stat2.getPath()).join().get().getValue(), "value-2".getBytes()); | ||
String seq2 = stat2.getPath().replace(basePath, ""); | ||
long n2 = Long.parseLong(seq2); | ||
|
||
assertNotEquals(seq1, seq2); | ||
assertNotEquals(n1, n2); | ||
} | ||
} |
Oops, something went wrong.