Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.bookkeeper.meta.zk;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.bookkeeper.util.BookKeeperConstants.AVAILABLE_NODE;
import static org.apache.bookkeeper.util.BookKeeperConstants.DISABLE_HEALTH_CHECK;
import static org.apache.bookkeeper.util.BookKeeperConstants.EMPTY_BYTE_ARRAY;
import static org.apache.bookkeeper.util.BookKeeperConstants.READONLY;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.conf.AbstractConfiguration;
import org.apache.bookkeeper.meta.AbstractZkLedgerManagerFactory;
import org.apache.bookkeeper.meta.HierarchicalLedgerManagerFactory;
import org.apache.bookkeeper.meta.LayoutManager;
import org.apache.bookkeeper.meta.LedgerManagerFactory;
import org.apache.bookkeeper.meta.LongHierarchicalLedgerManagerFactory;
import org.apache.bookkeeper.meta.ZkLayoutManager;
import org.apache.bookkeeper.meta.exceptions.Code;
import org.apache.bookkeeper.meta.exceptions.MetadataException;
import org.apache.bookkeeper.stats.StatsLogger;
import org.apache.bookkeeper.util.BookKeeperConstants;
import org.apache.bookkeeper.util.ZkUtils;
import org.apache.bookkeeper.zookeeper.RetryPolicy;
import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
/**
* This is a mixin class for supporting zookeeper based metadata driver.
*/
@Slf4j
public class ZKMetadataDriverBase implements AutoCloseable {
protected static final String SCHEME = "zk";
private static final int ZK_CLIENT_WAIT_FOR_SHUTDOWN_TIMEOUT_MS = 5000;
public static String getZKServersFromServiceUri(URI uri) {
String authority = uri.getAuthority();
if (authority == null) {
throw new IllegalArgumentException("Invalid metadata service URI format: " + uri);
}
return authority.replace(";", ",");
}
@SuppressWarnings("deprecation")
public static String resolveZkServers(AbstractConfiguration> conf) {
String metadataServiceUriStr = conf.getMetadataServiceUriUnchecked();
if (null == metadataServiceUriStr) {
return conf.getZkServers();
}
URI metadataServiceUri = URI.create(metadataServiceUriStr);
return getZKServersFromServiceUri(metadataServiceUri);
}
@SuppressWarnings("deprecation")
public static String resolveZkLedgersRootPath(AbstractConfiguration> conf) {
String metadataServiceUriStr = conf.getMetadataServiceUriUnchecked();
if (null == metadataServiceUriStr) {
return conf.getZkLedgersRootPath();
}
URI metadataServiceUri = URI.create(metadataServiceUriStr);
return metadataServiceUri.getPath();
}
@SuppressWarnings("deprecation")
public static Class extends LedgerManagerFactory> resolveLedgerManagerFactory(URI metadataServiceUri) {
checkNotNull(metadataServiceUri, "Metadata service uri is null");
String scheme = metadataServiceUri.getScheme();
checkNotNull(scheme, "Invalid metadata service : " + metadataServiceUri);
String[] schemeParts = StringUtils.split(scheme.toLowerCase(), '+');
checkArgument(SCHEME.equals(schemeParts[0]), "Unknown metadata service scheme found : "
+ schemeParts[0]);
Class extends LedgerManagerFactory> ledgerManagerFactoryClass;
if (schemeParts.length > 1) {
switch (schemeParts[1]) {
case org.apache.bookkeeper.meta.FlatLedgerManagerFactory.NAME:
ledgerManagerFactoryClass = org.apache.bookkeeper.meta.FlatLedgerManagerFactory.class;
break;
case HierarchicalLedgerManagerFactory.NAME:
ledgerManagerFactoryClass = HierarchicalLedgerManagerFactory.class;
break;
case LongHierarchicalLedgerManagerFactory.NAME:
ledgerManagerFactoryClass = LongHierarchicalLedgerManagerFactory.class;
break;
case org.apache.bookkeeper.meta.MSLedgerManagerFactory.NAME:
ledgerManagerFactoryClass = org.apache.bookkeeper.meta.MSLedgerManagerFactory.class;
break;
case "null":
// the ledger manager factory class is not set, so the client will be using the class that is
// recorded in ledger manager layout.
ledgerManagerFactoryClass = null;
break;
default:
throw new IllegalArgumentException("Unknown ledger manager type found '"
+ schemeParts[1] + "' at uri : " + metadataServiceUri);
}
} else {
// behave as in the +null case, infer the layout from the store or fall back to the default
ledgerManagerFactoryClass = null;
}
return ledgerManagerFactoryClass;
}
// URI
protected AbstractConfiguration> conf;
protected StatsLogger statsLogger;
// zookeeper related variables
protected List acls;
@Getter
@Setter
protected ZooKeeper zk = null;
// whether the zk handle is one we created, or is owned by whoever
// instantiated us
protected boolean ownZKHandle = false;
// disable health check path
String disableHealthCheckPath;
// ledgers root path
protected String ledgersRootPath;
// managers
protected LayoutManager layoutManager;
protected LedgerManagerFactory lmFactory;
public String getScheme() {
return SCHEME;
}
@SuppressWarnings("deprecation")
@SneakyThrows(InterruptedException.class)
protected void initialize(AbstractConfiguration> conf,
StatsLogger statsLogger,
RetryPolicy zkRetryPolicy,
Optional