
com.twitter.distributedlog.impl.ZKLogSegmentMetadataStore Maven / Gradle / Ivy
The newest version!
/**
* 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 com.twitter.distributedlog.impl;
import com.twitter.distributedlog.DistributedLogConfiguration;
import com.twitter.distributedlog.LogSegmentMetadata;
import com.twitter.distributedlog.ZooKeeperClient;
import com.twitter.distributedlog.callback.LogSegmentNamesListener;
import com.twitter.distributedlog.logsegment.LogSegmentMetadataStore;
import com.twitter.distributedlog.util.DLUtils;
import com.twitter.distributedlog.util.FutureUtils;
import com.twitter.distributedlog.util.OrderedScheduler;
import com.twitter.distributedlog.util.Transaction;
import com.twitter.distributedlog.zk.DefaultZKOp;
import com.twitter.distributedlog.zk.ZKOp;
import com.twitter.distributedlog.zk.ZKTransaction;
import com.twitter.distributedlog.zk.ZKVersionedSetOp;
import com.twitter.util.Future;
import com.twitter.util.FutureEventListener;
import com.twitter.util.Promise;
import org.apache.bookkeeper.meta.ZkVersion;
import org.apache.bookkeeper.versioning.Version;
import org.apache.bookkeeper.versioning.Versioned;
import org.apache.zookeeper.AsyncCallback.Children2Callback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Op;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static com.google.common.base.Charsets.UTF_8;
/**
* ZooKeeper based log segment metadata store.
*/
public class ZKLogSegmentMetadataStore implements LogSegmentMetadataStore, Watcher, Children2Callback {
private static final Logger logger = LoggerFactory.getLogger(ZKLogSegmentMetadataStore.class);
private static class ReadLogSegmentsTask implements Runnable, FutureEventListener> {
private final String logSegmentsPath;
private final ZKLogSegmentMetadataStore store;
private int currentZKBackOffMs;
ReadLogSegmentsTask(String logSegmentsPath,
ZKLogSegmentMetadataStore metadataStore) {
this.logSegmentsPath = logSegmentsPath;
this.store = metadataStore;
this.currentZKBackOffMs = store.minZKBackoffMs;
}
@Override
public void onSuccess(final List segments) {
// reset the back off after a successful operation
currentZKBackOffMs = store.minZKBackoffMs;
final Set listenerSet = store.listeners.get(logSegmentsPath);
if (null != listenerSet) {
store.submitTask(logSegmentsPath, new Runnable() {
@Override
public void run() {
for (LogSegmentNamesListener listener : listenerSet) {
listener.onSegmentsUpdated(segments);
}
}
});
}
}
@Override
public void onFailure(Throwable cause) {
int backoffMs = store.minZKBackoffMs;
if ((cause instanceof KeeperException)) {
KeeperException ke = (KeeperException) cause;
if (KeeperException.Code.NONODE == ke.code()) {
// the log segment has been deleted, remove all the registered listeners
store.listeners.remove(logSegmentsPath);
return;
}
backoffMs = currentZKBackOffMs;
currentZKBackOffMs = Math.min(2 * currentZKBackOffMs, store.maxZKBackoffMs);
}
store.scheduleTask(logSegmentsPath, this, backoffMs);
}
@Override
public void run() {
if (null != store.listeners.get(logSegmentsPath)) {
store.getLogSegmentNames(logSegmentsPath, store).addEventListener(this);
} else {
logger.debug("Log segments listener for {} has been removed.", logSegmentsPath);
}
}
}
final DistributedLogConfiguration conf;
// settings
final int minZKBackoffMs;
final int maxZKBackoffMs;
final boolean skipMinVersionCheck;
final ZooKeeperClient zkc;
// log segment listeners
final ConcurrentMap> listeners;
// scheduler
final OrderedScheduler scheduler;
final ReentrantReadWriteLock closeLock;
boolean closed = false;
public ZKLogSegmentMetadataStore(DistributedLogConfiguration conf,
ZooKeeperClient zkc,
OrderedScheduler scheduler) {
this.conf = conf;
this.zkc = zkc;
this.listeners = new ConcurrentHashMap>();
this.scheduler = scheduler;
this.closeLock = new ReentrantReadWriteLock();
// settings
this.minZKBackoffMs = conf.getZKRetryBackoffStartMillis();
this.maxZKBackoffMs = conf.getZKRetryBackoffMaxMillis();
this.skipMinVersionCheck = conf.getDLLedgerMetadataSkipMinVersionCheck();
}
protected void scheduleTask(Object key, Runnable r, long delayMs) {
closeLock.readLock().lock();
try {
if (closed) {
return;
}
scheduler.schedule(key, r, delayMs, TimeUnit.MILLISECONDS);
} finally {
closeLock.readLock().unlock();
}
}
protected void submitTask(Object key, Runnable r) {
closeLock.readLock().lock();
try {
if (closed) {
return;
}
scheduler.submit(key, r);
} finally {
closeLock.readLock().unlock();
}
}
// max sequence number and max transaction id
@Override
public void storeMaxLogSegmentSequenceNumber(Transaction
© 2015 - 2025 Weber Informatics LLC | Privacy Policy