You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by an...@apache.org on 2018/10/05 13:36:47 UTC
[19/51] [partial] zookeeper git commit: ZOOKEEPER-3032: MAVEN
MIGRATION - branch-3.5 - move java server, client
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/util/SerializeUtils.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/util/SerializeUtils.java b/src/java/main/org/apache/zookeeper/server/util/SerializeUtils.java
deleted file mode 100644
index eccf527..0000000
--- a/src/java/main/org/apache/zookeeper/server/util/SerializeUtils.java
+++ /dev/null
@@ -1,171 +0,0 @@
-/**
- * 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.zookeeper.server.util;
-
-import org.apache.jute.BinaryInputArchive;
-import org.apache.jute.BinaryOutputArchive;
-import org.apache.jute.InputArchive;
-import org.apache.jute.OutputArchive;
-import org.apache.jute.Record;
-import org.apache.zookeeper.ZooDefs.OpCode;
-import org.apache.zookeeper.common.IOUtils;
-import org.apache.zookeeper.server.DataTree;
-import org.apache.zookeeper.server.Request;
-import org.apache.zookeeper.server.ZooTrace;
-import org.apache.zookeeper.txn.CreateContainerTxn;
-import org.apache.zookeeper.txn.CreateSessionTxn;
-import org.apache.zookeeper.txn.CreateTTLTxn;
-import org.apache.zookeeper.txn.CreateTxn;
-import org.apache.zookeeper.txn.CreateTxnV0;
-import org.apache.zookeeper.txn.DeleteTxn;
-import org.apache.zookeeper.txn.ErrorTxn;
-import org.apache.zookeeper.txn.MultiTxn;
-import org.apache.zookeeper.txn.SetACLTxn;
-import org.apache.zookeeper.txn.SetDataTxn;
-import org.apache.zookeeper.txn.TxnHeader;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.EOFException;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-
-public class SerializeUtils {
- private static final Logger LOG = LoggerFactory.getLogger(SerializeUtils.class);
-
- public static Record deserializeTxn(byte txnBytes[], TxnHeader hdr)
- throws IOException {
- final ByteArrayInputStream bais = new ByteArrayInputStream(txnBytes);
- InputArchive ia = BinaryInputArchive.getArchive(bais);
-
- hdr.deserialize(ia, "hdr");
- bais.mark(bais.available());
- Record txn = null;
- switch (hdr.getType()) {
- case OpCode.createSession:
- // This isn't really an error txn; it just has the same
- // format. The error represents the timeout
- txn = new CreateSessionTxn();
- break;
- case OpCode.closeSession:
- return null;
- case OpCode.create:
- case OpCode.create2:
- txn = new CreateTxn();
- break;
- case OpCode.createTTL:
- txn = new CreateTTLTxn();
- break;
- case OpCode.createContainer:
- txn = new CreateContainerTxn();
- break;
- case OpCode.delete:
- case OpCode.deleteContainer:
- txn = new DeleteTxn();
- break;
- case OpCode.reconfig:
- case OpCode.setData:
- txn = new SetDataTxn();
- break;
- case OpCode.setACL:
- txn = new SetACLTxn();
- break;
- case OpCode.error:
- txn = new ErrorTxn();
- break;
- case OpCode.multi:
- txn = new MultiTxn();
- break;
- default:
- throw new IOException("Unsupported Txn with type=%d" + hdr.getType());
- }
- if (txn != null) {
- try {
- txn.deserialize(ia, "txn");
- } catch(EOFException e) {
- // perhaps this is a V0 Create
- if (hdr.getType() == OpCode.create) {
- CreateTxn create = (CreateTxn)txn;
- bais.reset();
- CreateTxnV0 createv0 = new CreateTxnV0();
- createv0.deserialize(ia, "txn");
- // cool now make it V1. a -1 parentCVersion will
- // trigger fixup processing in processTxn
- create.setPath(createv0.getPath());
- create.setData(createv0.getData());
- create.setAcl(createv0.getAcl());
- create.setEphemeral(createv0.getEphemeral());
- create.setParentCVersion(-1);
- } else {
- throw e;
- }
- }
- }
- return txn;
- }
-
- public static void deserializeSnapshot(DataTree dt,InputArchive ia,
- Map<Long, Integer> sessions) throws IOException {
- int count = ia.readInt("count");
- while (count > 0) {
- long id = ia.readLong("id");
- int to = ia.readInt("timeout");
- sessions.put(id, to);
- if (LOG.isTraceEnabled()) {
- ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK,
- "loadData --- session in archive: " + id
- + " with timeout: " + to);
- }
- count--;
- }
- dt.deserialize(ia, "tree");
- }
-
- public static void serializeSnapshot(DataTree dt,OutputArchive oa,
- Map<Long, Integer> sessions) throws IOException {
- HashMap<Long, Integer> sessSnap = new HashMap<Long, Integer>(sessions);
- oa.writeInt(sessSnap.size(), "count");
- for (Entry<Long, Integer> entry : sessSnap.entrySet()) {
- oa.writeLong(entry.getKey().longValue(), "id");
- oa.writeInt(entry.getValue().intValue(), "timeout");
- }
- dt.serialize(oa, "tree");
- }
-
- public static byte[] serializeRequest(Request request) {
- if (request == null || request.getHdr() == null) return null;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
- try {
- request.getHdr().serialize(boa, "hdr");
- if (request.getTxn() != null) {
- request.getTxn().serialize(boa, "txn");
- }
- } catch (IOException e) {
- LOG.error("This really should be impossible", e);
- } finally {
- IOUtils.cleanup(LOG, baos);
- }
- return baos.toByteArray();
- }
-}
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/util/VerifyingFileFactory.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/util/VerifyingFileFactory.java b/src/java/main/org/apache/zookeeper/server/util/VerifyingFileFactory.java
deleted file mode 100644
index 0e93159..0000000
--- a/src/java/main/org/apache/zookeeper/server/util/VerifyingFileFactory.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/**
- * 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.zookeeper.server.util;
-
-import java.io.File;
-
-import org.slf4j.Logger;
-
-public final class VerifyingFileFactory {
-
- private final boolean warnForRelativePath;
- private final boolean failForNonExistingPath;
- private final Logger log;
-
- public VerifyingFileFactory(Builder builder){
- warnForRelativePath = builder.warnForRelativePathOption;
- failForNonExistingPath = builder.failForNonExistingPathOption;
- log = builder.log;
- assert(log != null);
- }
-
- public File create(String path) {
- File file = new File(path);
- return validate(file);
- }
-
- public File validate(File file) {
- if(warnForRelativePath) doWarnForRelativePath(file);
- if(failForNonExistingPath) doFailForNonExistingPath(file);
- return file;
- }
-
- private void doFailForNonExistingPath(File file) {
- if (!file.exists()) {
- throw new IllegalArgumentException(file.toString()
- + " file is missing");
- }
- }
-
- private void doWarnForRelativePath(File file) {
- if(file.isAbsolute()) return;
- if(file.getPath().substring(0, 2).equals("."+File.separator)) return;
- log.warn(file.getPath()+" is relative. Prepend ."
- +File.separator+" to indicate that you're sure!");
- }
-
- public static class Builder {
- private boolean warnForRelativePathOption = false;
- private boolean failForNonExistingPathOption = false;
- private final Logger log;
-
- public Builder(Logger log){
- this.log = log;
- }
-
- public Builder warnForRelativePath() {
- warnForRelativePathOption = true;
- return this;
- }
-
- public Builder failForNonExistingPath() {
- failForNonExistingPathOption = true;
- return this;
- }
-
- public VerifyingFileFactory build() {
- return new VerifyingFileFactory(this);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/util/ZxidUtils.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/util/ZxidUtils.java b/src/java/main/org/apache/zookeeper/server/util/ZxidUtils.java
deleted file mode 100644
index 39e076a..0000000
--- a/src/java/main/org/apache/zookeeper/server/util/ZxidUtils.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/**
- * 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.zookeeper.server.util;
-
-public class ZxidUtils {
- static public long getEpochFromZxid(long zxid) {
- return zxid >> 32L;
- }
- static public long getCounterFromZxid(long zxid) {
- return zxid & 0xffffffffL;
- }
- static public long makeZxid(long epoch, long counter) {
- return (epoch << 32L) | (counter & 0xffffffffL);
- }
- static public String zxidToString(long zxid) {
- return Long.toHexString(zxid);
- }
-}
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/util/SecurityUtils.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/util/SecurityUtils.java b/src/java/main/org/apache/zookeeper/util/SecurityUtils.java
deleted file mode 100644
index 67484e4..0000000
--- a/src/java/main/org/apache/zookeeper/util/SecurityUtils.java
+++ /dev/null
@@ -1,298 +0,0 @@
-/**
- * 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.zookeeper.util;
-
-import java.security.Principal;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
-
-import javax.security.auth.Subject;
-import javax.security.auth.callback.CallbackHandler;
-import javax.security.sasl.Sasl;
-import javax.security.sasl.SaslClient;
-import javax.security.sasl.SaslException;
-import javax.security.sasl.SaslServer;
-
-import org.apache.zookeeper.SaslClientCallbackHandler;
-import org.apache.zookeeper.server.auth.KerberosName;
-import org.ietf.jgss.GSSContext;
-import org.ietf.jgss.GSSCredential;
-import org.ietf.jgss.GSSException;
-import org.ietf.jgss.GSSManager;
-import org.ietf.jgss.GSSName;
-import org.ietf.jgss.Oid;
-import org.slf4j.Logger;
-
-public final class SecurityUtils {
-
- public static final String QUORUM_HOSTNAME_PATTERN = "_HOST";
-
- /**
- * Create an instance of a SaslClient. It will return null if there is an exception.
- *
- * @param subject subject
- * @param servicePrincipal principal
- * @param protocol name of the protocol for which the authentication is being performed
- * @param serverName name of the server to authenticate to
- * @param LOG logger
- * @param entity can be either zookeeper client or quorum learner
- *
- * @return saslclient object
- * @throws SaslException
- */
- public static SaslClient createSaslClient(final Subject subject,
- final String servicePrincipal, final String protocol,
- final String serverName, final Logger LOG, final String entity) throws SaslException {
- SaslClient saslClient;
- // Use subject.getPrincipals().isEmpty() as an indication of which SASL
- // mechanism to use: if empty, use DIGEST-MD5; otherwise, use GSSAPI.
- if (subject.getPrincipals().isEmpty()) {
- // no principals: must not be GSSAPI: use DIGEST-MD5 mechanism
- // instead.
- LOG.info("{} will use DIGEST-MD5 as SASL mechanism.", entity);
- String[] mechs = { "DIGEST-MD5" };
- String username = (String) (subject.getPublicCredentials()
- .toArray()[0]);
- String password = (String) (subject.getPrivateCredentials()
- .toArray()[0]);
- // 'domain' parameter is hard-wired between the server and client
- saslClient = Sasl.createSaslClient(mechs, username, protocol,
- serverName, null, new SaslClientCallbackHandler(password, entity));
- return saslClient;
- } else { // GSSAPI.
- final Object[] principals = subject.getPrincipals().toArray();
- // determine client principal from subject.
- final Principal clientPrincipal = (Principal) principals[0];
- boolean usingNativeJgss = Boolean
- .getBoolean("sun.security.jgss.native");
- if (usingNativeJgss) {
- // http://docs.oracle.com/javase/6/docs/technotes/guides/security/jgss/jgss-features.html
- // """
- // In addition, when performing operations as a particular
- // Subject, e.g. Subject.doAs(...) or
- // Subject.doAsPrivileged(...),
- // the to-be-used GSSCredential should be added to Subject's
- // private credential set. Otherwise, the GSS operations will
- // fail since no credential is found.
- // """
- try {
- GSSManager manager = GSSManager.getInstance();
- Oid krb5Mechanism = new Oid("1.2.840.113554.1.2.2");
- GSSCredential cred = manager.createCredential(null,
- GSSContext.DEFAULT_LIFETIME, krb5Mechanism,
- GSSCredential.INITIATE_ONLY);
- subject.getPrivateCredentials().add(cred);
- LOG.debug("Added private credential to {} principal name: '{}'",
- entity, clientPrincipal);
- } catch (GSSException ex) {
- LOG.warn("Cannot add private credential to subject; "
- + "authentication at the server may fail", ex);
- }
- }
- final KerberosName clientKerberosName = new KerberosName(
- clientPrincipal.getName());
- // assume that server and client are in the same realm (by default;
- // unless the system property
- // "zookeeper.server.realm" is set).
- String serverRealm = System.getProperty("zookeeper.server.realm",
- clientKerberosName.getRealm());
- KerberosName serviceKerberosName = new KerberosName(
- servicePrincipal + "@" + serverRealm);
- final String serviceName = serviceKerberosName.getServiceName();
- final String serviceHostname = serviceKerberosName.getHostName();
- final String clientPrincipalName = clientKerberosName.toString();
- try {
- saslClient = Subject.doAs(subject,
- new PrivilegedExceptionAction<SaslClient>() {
- public SaslClient run() throws SaslException {
- LOG.info("{} will use GSSAPI as SASL mechanism.", entity);
- String[] mechs = { "GSSAPI" };
- LOG.debug("creating sasl client: {}={};service={};serviceHostname={}",
- new Object[] { entity, clientPrincipalName, serviceName, serviceHostname });
- SaslClient saslClient = Sasl.createSaslClient(
- mechs, clientPrincipalName, serviceName,
- serviceHostname, null,
- new SaslClientCallbackHandler(null, entity));
- return saslClient;
- }
- });
- return saslClient;
- } catch (Exception e) {
- LOG.error("Exception while trying to create SASL client", e);
- return null;
- }
- }
- }
-
- /**
- * Create an instance of a SaslServer. It will return null if there is an exception.
- *
- * @param subject subject
- * @param protocol protocol
- * @param serverName server name
- * @param callbackHandler login callback handler
- * @param LOG logger
- * @return sasl server object
- */
- public static SaslServer createSaslServer(final Subject subject,
- final String protocol, final String serverName,
- final CallbackHandler callbackHandler, final Logger LOG) {
- if (subject != null) {
- // server is using a JAAS-authenticated subject: determine service
- // principal name and hostname from zk server's subject.
- if (subject.getPrincipals().size() > 0) {
- try {
- final Object[] principals = subject.getPrincipals()
- .toArray();
- final Principal servicePrincipal = (Principal) principals[0];
-
- // e.g. servicePrincipalNameAndHostname :=
- // "zookeeper/myhost.foo.com@FOO.COM"
- final String servicePrincipalNameAndHostname = servicePrincipal
- .getName();
-
- int indexOf = servicePrincipalNameAndHostname.indexOf("/");
-
- // e.g. servicePrincipalName := "zookeeper"
- final String servicePrincipalName = servicePrincipalNameAndHostname
- .substring(0, indexOf);
-
- // e.g. serviceHostnameAndKerbDomain :=
- // "myhost.foo.com@FOO.COM"
- final String serviceHostnameAndKerbDomain = servicePrincipalNameAndHostname
- .substring(indexOf + 1,
- servicePrincipalNameAndHostname.length());
-
- indexOf = serviceHostnameAndKerbDomain.indexOf("@");
- // e.g. serviceHostname := "myhost.foo.com"
- final String serviceHostname = serviceHostnameAndKerbDomain
- .substring(0, indexOf);
-
- // TODO: should depend on zoo.cfg specified mechs, but if
- // subject is non-null, it can be assumed to be GSSAPI.
- final String mech = "GSSAPI";
-
- LOG.debug("serviceHostname is '" + serviceHostname + "'");
- LOG.debug("servicePrincipalName is '" + servicePrincipalName
- + "'");
- LOG.debug("SASL mechanism(mech) is '" + mech + "'");
-
- boolean usingNativeJgss = Boolean
- .getBoolean("sun.security.jgss.native");
- if (usingNativeJgss) {
- // http://docs.oracle.com/javase/6/docs/technotes/guides/security/jgss/jgss-features.html
- // """
- // In addition, when performing operations as a
- // particular
- // Subject, e.g. Subject.doAs(...) or
- // Subject.doAsPrivileged(...), the to-be-used
- // GSSCredential should be added to Subject's
- // private credential set. Otherwise, the GSS operations
- // will fail since no credential is found.
- // """
- try {
- GSSManager manager = GSSManager.getInstance();
- Oid krb5Mechanism = new Oid("1.2.840.113554.1.2.2");
- GSSName gssName = manager.createName(
- servicePrincipalName + "@"
- + serviceHostname,
- GSSName.NT_HOSTBASED_SERVICE);
- GSSCredential cred = manager.createCredential(
- gssName, GSSContext.DEFAULT_LIFETIME,
- krb5Mechanism, GSSCredential.ACCEPT_ONLY);
- subject.getPrivateCredentials().add(cred);
- LOG.debug("Added private credential to service principal name: '{}',"
- + " GSSCredential name: {}", servicePrincipalName, cred.getName());
- } catch (GSSException ex) {
- LOG.warn("Cannot add private credential to subject; "
- + "clients authentication may fail", ex);
- }
- }
- try {
- return Subject.doAs(subject,
- new PrivilegedExceptionAction<SaslServer>() {
- public SaslServer run() {
- try {
- SaslServer saslServer;
- saslServer = Sasl.createSaslServer(
- mech, servicePrincipalName,
- serviceHostname, null,
- callbackHandler);
- return saslServer;
- } catch (SaslException e) {
- LOG.error("Zookeeper Server failed to create a SaslServer to interact with a client during session initiation: ", e);
- return null;
- }
- }
- });
- } catch (PrivilegedActionException e) {
- // TODO: exit server at this point(?)
- LOG.error("Zookeeper Quorum member experienced a PrivilegedActionException exception while creating a SaslServer using a JAAS principal context:", e);
- }
- } catch (IndexOutOfBoundsException e) {
- LOG.error("server principal name/hostname determination error: ", e);
- }
- } else {
- // JAAS non-GSSAPI authentication: assuming and supporting only
- // DIGEST-MD5 mechanism for now.
- // TODO: use 'authMech=' value in zoo.cfg.
- try {
- SaslServer saslServer = Sasl.createSaslServer("DIGEST-MD5",
- protocol, serverName, null, callbackHandler);
- return saslServer;
- } catch (SaslException e) {
- LOG.error("Zookeeper Quorum member failed to create a SaslServer to interact with a client during session initiation", e);
- }
- }
- }
- return null;
- }
-
- /**
- * Convert Kerberos principal name pattern to valid Kerberos principal name.
- * If the principal name contains hostname pattern "_HOST" then it replaces
- * with the given hostname, which should be fully-qualified domain name.
- *
- * @param principalConfig
- * the Kerberos principal name conf value to convert
- * @param hostname
- * the fully-qualified domain name used for substitution
- * @return converted Kerberos principal name
- */
- public static String getServerPrincipal(String principalConfig,
- String hostname) {
- String[] components = getComponents(principalConfig);
- if (components == null || components.length != 2
- || !components[1].equals(QUORUM_HOSTNAME_PATTERN)) {
- return principalConfig;
- } else {
- return replacePattern(components, hostname);
- }
- }
-
- private static String[] getComponents(String principalConfig) {
- if (principalConfig == null)
- return null;
- return principalConfig.split("[/]");
- }
-
- private static String replacePattern(String[] components, String hostname) {
- return components[0] + "/" + hostname.toLowerCase();
- }
-}
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/version/util/VerGen.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/version/util/VerGen.java b/src/java/main/org/apache/zookeeper/version/util/VerGen.java
deleted file mode 100644
index 014f01d..0000000
--- a/src/java/main/org/apache/zookeeper/version/util/VerGen.java
+++ /dev/null
@@ -1,167 +0,0 @@
-/**
- * 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.zookeeper.version.util;
-
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-public class VerGen {
- private static final String PACKAGE_NAME = "org.apache.zookeeper.version";
- private static final String TYPE_NAME = "Info";
-
- static void printUsage() {
- System.out.print("Usage:\tjava -cp <classpath> org.apache.zookeeper."
- + "version.util.VerGen maj.min.micro[-qualifier] rev buildDate");
- System.exit(1);
- }
-
- public static void generateFile(File outputDir, Version version, String rev, String buildDate)
- {
- String path = PACKAGE_NAME.replaceAll("\\.", "/");
- File pkgdir = new File(outputDir, path);
- if (!pkgdir.exists()) {
- // create the pkg directory
- boolean ret = pkgdir.mkdirs();
- if (!ret) {
- System.out.println("Cannnot create directory: " + path);
- System.exit(1);
- }
- } else if (!pkgdir.isDirectory()) {
- // not a directory
- System.out.println(path + " is not a directory.");
- System.exit(1);
- }
-
- try (FileWriter w = new FileWriter(new File(pkgdir, TYPE_NAME + ".java"))) {
- w.write("// Do not edit!\n// File generated by org.apache.zookeeper"
- + ".version.util.VerGen.\n");
- w.write("/**\n");
- w.write("* Licensed to the Apache Software Foundation (ASF) under one\n");
- w.write("* or more contributor license agreements. See the NOTICE file\n");
- w.write("* distributed with this work for additional information\n");
- w.write("* regarding copyright ownership. The ASF licenses this file\n");
- w.write("* to you under the Apache License, Version 2.0 (the\n");
- w.write("* \"License\"); you may not use this file except in compliance\n");
- w.write("* with the License. You may obtain a copy of the License at\n");
- w.write("*\n");
- w.write("* http://www.apache.org/licenses/LICENSE-2.0\n");
- w.write("*\n");
- w.write("* Unless required by applicable law or agreed to in writing, software\n");
- w.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n");
- w.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n");
- w.write("* See the License for the specific language governing permissions and\n");
- w.write("* limitations under the License.\n");
- w.write("*/\n");
- w.write("\n");
- w.write("package " + PACKAGE_NAME + ";\n\n");
- w.write("public interface " + TYPE_NAME + " {\n");
- w.write(" int MAJOR=" + version.maj + ";\n");
- w.write(" int MINOR=" + version.min + ";\n");
- w.write(" int MICRO=" + version.micro + ";\n");
- w.write(" String QUALIFIER="
- + (version.qualifier == null ? null :
- "\"" + version.qualifier + "\"")
- + ";\n");
- if (rev.equals("-1")) {
- System.out.println("Unknown REVISION number, using " + rev);
- }
- w.write(" int REVISION=-1; //TODO: remove as related to SVN VCS\n");
- w.write(" String REVISION_HASH=\"" + rev + "\";\n");
- w.write(" String BUILD_DATE=\"" + buildDate
- + "\";\n");
- w.write("}\n");
- } catch (IOException e) {
- System.out.println("Unable to generate version.Info file: "
- + e.getMessage());
- System.exit(1);
- }
- }
-
- public static class Version {
- public int maj;
- public int min;
- public int micro;
- public String qualifier;
- }
-
- public static Version parseVersionString(String input) {
- Version result = new Version();
-
- Pattern p = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)((\\.\\d+)*)(-(.+))?$");
- Matcher m = p.matcher(input);
-
- if (!m.matches()) {
- return null;
- }
- result.maj = Integer.parseInt(m.group(1));
- result.min = Integer.parseInt(m.group(2));
- result.micro = Integer.parseInt(m.group(3));
- if (m.groupCount() == 7) {
- result.qualifier = m.group(7);
- } else {
- result.qualifier = null;
- }
- return result;
- }
-
- /**
- * Emits a org.apache.zookeeper.version.Info interface file with version and
- * revision information constants set to the values passed in as command
- * line parameters. The file is created in the current directory. <br>
- * Usage: java org.apache.zookeeper.version.util.VerGen maj.min.micro[-qualifier]
- * rev buildDate
- *
- * @param args
- * <ul>
- * <li>maj - major version number
- * <li>min - minor version number
- * <li>micro - minor minor version number
- * <li>qualifier - optional qualifier (dash followed by qualifier text)
- * <li>rev - current Git revision number
- * <li>buildDate - date the build
- * </ul>
- */
- public static void main(String[] args) {
- if (args.length != 3)
- printUsage();
- try {
- Version version = parseVersionString(args[0]);
- if (version == null) {
- System.err.println(
- "Invalid version number format, must be \"x.y.z(-.*)?\"");
- System.exit(1);
- }
- String rev = args[1];
- if (rev == null || rev.trim().isEmpty()) {
- rev = "-1";
- } else {
- rev = rev.trim();
- }
- generateFile(new File("."), version, rev, args[2]);
- } catch (NumberFormatException e) {
- System.err.println(
- "All version-related parameters must be valid integers!");
- throw e;
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/overview.html
----------------------------------------------------------------------
diff --git a/src/java/main/overview.html b/src/java/main/overview.html
deleted file mode 100644
index db9b16f..0000000
--- a/src/java/main/overview.html
+++ /dev/null
@@ -1,26 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-<!--
- 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.
--->
-
-<html>
-<head>
- <title>ZooKeeper</title>
-</head>
-<body>
-ZooKeeper is a service for coordinating processes of distributed applications.
-</body>
-</html>
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/systest/README.txt
----------------------------------------------------------------------
diff --git a/src/java/systest/README.txt b/src/java/systest/README.txt
deleted file mode 100644
index 8a2985c..0000000
--- a/src/java/systest/README.txt
+++ /dev/null
@@ -1,62 +0,0 @@
-To run the system test we need to create processing containers that we can
-spawn tasks, called Instances, in. (how is that for a dangling preposition!?!)
-Start up InstanceContainers first. Then run the system test. The system test
-finds the InstanceContainers and directs them through ZooKeeper, so you are
-going to need an instance of ZooKeeper running that they can all access.
-The easiest way to do all of this is to use the zookeeper fat jar.
-
-Steps to run system test
-------------------------
-1) transfer the fatjar from the release directory to all systems
- participating in the test. fatjar is in contrib/fatjar directory.
-
- (developers can generate by running "ant jar compile-test"
- targets in trunk, then compiling using "ant jar" in src/contrib/jarjar)
-
-2) run a zookeeper standalone instance (cluster is ok too)
-
-e.g. java -jar zookeeper-<version>-fatjar.jar server <zoo.cfg>
-
-Note: you must provide zoo.cfg, see sample in conf directory
-
-3) on each host start the system test container
-
-e.g. java -jar zookeeper-<version>-fatjar.jar ic <name> <zkHostPort> /sysTest
-
-name : name of the test container - must be unique
- typically it's a good idea to name after the host to aid debugging
-
-zkHostPort : the host:port of the server from step 2)
-
-4) initiate the system test using the fatjar:
-
-java -jar build/contrib/fatjar/zookeeper-<version>-fatjar.jar systest org.apache.zookeeper.test.system.SimpleSysTest
-
-by default it will access the zk server started in 2) on localhost:2181
-
-or you can specify a remote host:port using
- -DsysTest.zkHostPort=<host>:<port>,<host>:<port>,...
-
-java -DsysTest.zkHostPort=hostA:2181 -jar build/contrib/fatjar/zookeeper-<version>-fatjar.jar systest org.apache.zookeeper.test.system.SimpleSysTest
-
-where hostA is running the zk server started in step 2) above
-
-InstanceContainers can also be used to run a the saturation benchmark. The
-first two steps are the same as the system test. Step 3 is almost the same:
-
-3) start the InstanceContainer on each host:
-
-e.g. java -jar zookeeper-<version>-fatjar.jar ic <name> <zkHostPort> <prefix>
-
-note prefix can be /sysTest or any other path. If you do use /sysTest, make
-sure the system test isn't running when you run the benchmark.
-
-4) run GenerateLoad using the following
-
-java -jar build/contrib/fatjar/zookeeper-<version>-fatjar.jar generateLoad <zkHostPort> <prefix> #servers #clients
-
-Once GenerateLoad is started, it will read commands from stdin. Usually
-the only command you need to know is "percentage" which sets the percentage
-of writes to use in the requests. Once a percentage is set, the benchmark
-will start. "percentage 0" will cause only reads to be issued and
-"percentage 100" will cause only writes to be issued.
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/systest/org/apache/zookeeper/test/system/BaseSysTest.java
----------------------------------------------------------------------
diff --git a/src/java/systest/org/apache/zookeeper/test/system/BaseSysTest.java b/src/java/systest/org/apache/zookeeper/test/system/BaseSysTest.java
deleted file mode 100644
index 2ed516c..0000000
--- a/src/java/systest/org/apache/zookeeper/test/system/BaseSysTest.java
+++ /dev/null
@@ -1,277 +0,0 @@
-/**
- * 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.zookeeper.test.system;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.UnknownHostException;
-import java.util.HashMap;
-
-import org.apache.zookeeper.WatchedEvent;
-import org.apache.zookeeper.Watcher;
-import org.apache.zookeeper.ZooKeeper;
-import org.apache.zookeeper.server.quorum.QuorumPeer;
-import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.runner.JUnitCore;
-
-@Ignore("No tests in this class.")
-public class BaseSysTest {
- private static final File testData = new File(
- System.getProperty("test.data.dir", "build/test/data"));
- private static int fakeBasePort = 33222;
- private static String zkHostPort;
- protected String prefix = "/sysTest";
- ZooKeeper zk;
- static {
- try {
- zkHostPort = System.getProperty("sysTest.zkHostPort", InetAddress.getLocalHost().getCanonicalHostName() + ":2181");
- } catch (UnknownHostException e) {
- e.printStackTrace();
- }
- }
- InstanceManager im;
- @Before
- public void setUp() throws Exception {
- if (!fakeMachines) {
- zk = new ZooKeeper(zkHostPort, 15000, new Watcher() {public void process(WatchedEvent e){}});
- im = new InstanceManager(zk, prefix);
- }
- }
- @After
- public void tearDown() throws Exception {
- if (null != im) {
- im.close();
- }
- }
-
- int serverCount = defaultServerCount;
- int clientCount = defaultClientCount;
- static int defaultServerCount = 5;
- static int defaultClientCount = 7;
- static {
- defaultServerCount = Integer.parseInt(System.getProperty("simpleSysTest.defaultServerCount", Integer.toString(defaultServerCount)));
- defaultClientCount = Integer.parseInt(System.getProperty("simpleSysTest.defaultClientCount", Integer.toString(defaultClientCount)));
- }
-
- String serverHostPort;
- String quorumHostPort;
- public String getHostPort() {
- return serverHostPort;
- }
- public int getServerCount() {
- return serverCount;
- }
- public int getClientCount() {
- return clientCount;
- }
-
- public void startServers() throws IOException {
- for(int i = 0; i < serverCount; i++) {
- startServer(i);
- }
- }
- public void stopServers() throws IOException {
- for(int i = 0; i < serverCount; i++) {
- stopServer(i);
- }
- }
- public void startClients() throws IOException {
- for(int i = 0; i < clientCount; i++) {
- startClient(i);
- }
- }
- public void stopClients() throws IOException {
- for(int i = 0; i < clientCount; i++) {
- stopClient(i);
- }
- }
-
- private static boolean fakeMachines = System.getProperty("baseSysTest.fakeMachines", "no").equals("yes");
-
- public void configureServers(int count) throws Exception {
- serverCount = count;
- if (fakeMachines) {
- fakeConfigureServers(count);
- } else {
- distributedConfigureServers(count);
- }
- }
-
- private void distributedConfigureServers(int count) throws IOException {
- StringBuilder sbClient = new StringBuilder();
- StringBuilder sbServer = new StringBuilder();
- try {
- for(int i = 0; i < count; i++) {
- String r[] = QuorumPeerInstance.createServer(im, i);
- if (i > 0) {
- sbClient.append(',');
- sbServer.append(',');
- }
- sbClient.append(r[0]); // r[0] == "host:clientPort"
- sbServer.append(r[1]); // r[1] == "host:leaderPort:leaderElectionPort"
- sbServer.append(";"+(r[0].split(":"))[1]); // Appending ";clientPort"
- }
- serverHostPort = sbClient.toString();
- quorumHostPort = sbServer.toString();
- } catch(Exception e) {
- IOException ioe = new IOException(e.getMessage());
- ioe.setStackTrace(e.getStackTrace());
- throw ioe;
- }
- }
-
- private QuorumPeer qps[];
- private File qpsDirs[];
- HashMap<Long,QuorumServer> peers;
- private void fakeConfigureServers(int count) throws IOException {
- peers = new HashMap<Long,QuorumServer>();
- qps = new QuorumPeer[count];
- qpsDirs = new File[count];
- for(int i = 1; i <= count; i++) {
- InetSocketAddress peerAddress = new InetSocketAddress("127.0.0.1",
- fakeBasePort + i);
- InetSocketAddress electionAddr = new InetSocketAddress("127.0.0.1",
- serverCount + fakeBasePort + i);
- peers.put(Long.valueOf(i), new QuorumServer(i, peerAddress,
- electionAddr));
- }
- StringBuilder sb = new StringBuilder();
- for(int i = 0; i < count; i++) {
- //make that testData exists otherwise it fails on windows
- testData.mkdirs();
- qpsDirs[i] = File.createTempFile("sysTest", ".tmp", testData);
- qpsDirs[i].delete();
- qpsDirs[i].mkdir();
- int port = fakeBasePort+10+i;
- if (sb.length() > 0) {
- sb.append(',');
- }
- sb.append("localhost:");
- sb.append(port);
- }
- serverHostPort = sb.toString();
- }
- final static int tickTime = 2000;
- final static int initLimit = 3;
- final static int syncLimit = 3;
-
- public void startServer(int index) throws IOException {
- int port = fakeBasePort+10+index;
- if (fakeMachines) {
- qps[index] = new QuorumPeer(peers, qpsDirs[index], qpsDirs[index], port, 0, index+1, tickTime, initLimit, syncLimit);
- qps[index].start();
- } else {
- try {
- QuorumPeerInstance.startInstance(im, quorumHostPort, index);
- } catch(Exception e) {
- IOException ioe = new IOException(e.getClass().getName() + ": " + e.getMessage());
- ioe.setStackTrace(e.getStackTrace());
- throw ioe;
- }
- }
- }
- public void stopServer(int index) throws IOException {
- if (fakeMachines) {
- qps[index].shutdown();
- } else {
- try {
- QuorumPeerInstance.stopInstance(im, index);
- } catch(Exception e) {
- IOException ioe = new IOException(e.getMessage());
- ioe.setStackTrace(e.getStackTrace());
- throw ioe;
- }
- }
- }
-
- public void configureClients(int count, Class<? extends Instance> clazz, String params) throws Exception {
- clientCount = count;
- if (fakeMachines) {
- fakeConfigureClients(count, clazz, params);
- } else {
- distributedConfigureClients(count, clazz, params);
- }
- }
- private Class<? extends Instance> clazz;
- String params;
- private void distributedConfigureClients(int count, Class<? extends Instance> clazz, String params) throws IOException {
- this.clazz = clazz;
- this.params = params;
-
- }
- private Instance fakeBaseClients[];
- private void fakeConfigureClients(int count, Class<? extends Instance> clazz, String params) throws IOException, ClassNotFoundException {
- fakeBaseClients = new Instance[count];
- for(int i = 0; i < count; i++) {
- try {
- fakeBaseClients[i] = clazz.newInstance();
- } catch (InstantiationException e) {
- e.printStackTrace();
- return;
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- return;
- }
- fakeBaseClients[i].configure(i + " " + params);
- }
- }
- public void startClient(int index) throws IOException {
- if (fakeMachines) {
- fakeStartClient(index);
- } else {
- distributedStartClient(index);
- }
- }
- private void distributedStartClient(int index) throws IOException {
- try {
- im.assignInstance("client" + index, clazz, index + " " + params, 1);
- } catch (Exception e) {
- throw new IOException(e.getMessage());
- }
- }
- private void fakeStartClient(int index) {
- fakeBaseClients[index].start();
- }
- public void stopClient(int index) throws IOException {
- if (fakeMachines) {
- fakeStopClient(index);
- } else {
- distributedStopClient(index);
- }
- }
- private void distributedStopClient(int index) throws IOException {
- try {
- im.removeInstance("client"+index);
- } catch (Exception e) {
- throw new IOException(e.getMessage());
- }
- }
- private void fakeStopClient(int index) {
- fakeBaseClients[index].stop();
- }
-
- static public void main(String args[]) {
- JUnitCore.main(args);
- }
-}
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/systest/org/apache/zookeeper/test/system/DuplicateNameException.java
----------------------------------------------------------------------
diff --git a/src/java/systest/org/apache/zookeeper/test/system/DuplicateNameException.java b/src/java/systest/org/apache/zookeeper/test/system/DuplicateNameException.java
deleted file mode 100644
index 8a96c57..0000000
--- a/src/java/systest/org/apache/zookeeper/test/system/DuplicateNameException.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/**
- * 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.zookeeper.test.system;
-
-public class DuplicateNameException extends Exception {
- private static final long serialVersionUID = 1L;
- public DuplicateNameException(String mess) {
- super(mess);
- }
-}
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/systest/org/apache/zookeeper/test/system/GenerateLoad.java
----------------------------------------------------------------------
diff --git a/src/java/systest/org/apache/zookeeper/test/system/GenerateLoad.java b/src/java/systest/org/apache/zookeeper/test/system/GenerateLoad.java
deleted file mode 100644
index 768d236..0000000
--- a/src/java/systest/org/apache/zookeeper/test/system/GenerateLoad.java
+++ /dev/null
@@ -1,718 +0,0 @@
-/**
- * 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.zookeeper.test.system;
-
-import java.io.BufferedReader;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.PrintStream;
-import java.net.InetAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Random;
-import java.util.Set;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.zookeeper.AsyncCallback.DataCallback;
-import org.apache.zookeeper.AsyncCallback.StatCallback;
-import org.apache.zookeeper.KeeperException;
-import org.apache.zookeeper.Watcher;
-import org.apache.zookeeper.Watcher.Event.EventType;
-import org.apache.zookeeper.Watcher.Event.KeeperState;
-import org.apache.zookeeper.CreateMode;
-import org.apache.zookeeper.ZooDefs.Ids;
-import org.apache.zookeeper.ZooKeeper;
-import org.apache.zookeeper.data.Stat;
-import org.apache.zookeeper.WatchedEvent;
-import org.apache.zookeeper.common.Time;
-
-
-public class GenerateLoad {
- protected static final Logger LOG = LoggerFactory.getLogger(GenerateLoad.class);
-
- static ServerSocket ss;
-
- static Set<SlaveThread> slaves = Collections
- .synchronizedSet(new HashSet<SlaveThread>());
-
- static Map<Long, Long> totalByTime = new HashMap<Long, Long>();
-
- volatile static long currentInterval;
-
- static long lastChange;
-
- static PrintStream sf;
- static PrintStream tf;
- static {
- try {
- tf = new PrintStream(new FileOutputStream("trace"));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
-
- static final int INTERVAL = 6000;
-
- synchronized static void add(long time, int count, Socket s) {
- long interval = time / INTERVAL;
- if (currentInterval == 0 || currentInterval > interval) {
- System.out.println("Dropping " + count + " for " + new Date(time)
- + " " + currentInterval + ">" + interval);
- return;
- }
- // We track totals by seconds
- Long total = totalByTime.get(interval);
- if (total == null) {
- totalByTime.put(interval, (long) count);
- } else {
- totalByTime.put(interval, total.longValue() + count);
- }
- tf.println(interval + " " + count + " " + s);
- }
-
- synchronized static long remove(long interval) {
- Long total = totalByTime.remove(interval);
- return total == null ? -1 : total;
- }
-
- static class SlaveThread extends Thread {
- Socket s;
-
- SlaveThread(Socket s) {
- setDaemon(true);
- this.s = s;
- start();
- }
-
- public void run() {
- try {
- System.out.println("Connected to " + s);
- BufferedReader is = new BufferedReader(new InputStreamReader(s
- .getInputStream()));
- String result;
- while ((result = is.readLine()) != null) {
- String timePercentCount[] = result.split(" ");
- if (timePercentCount.length != 5) {
- System.err.println("Got " + result + " from " + s
- + " exitng.");
- throw new IOException(result);
- }
- long time = Long.parseLong(timePercentCount[0]);
- // int percent = Integer.parseInt(timePercentCount[1]);
- int count = Integer.parseInt(timePercentCount[2]);
- int errs = Integer.parseInt(timePercentCount[3]);
- if (errs > 0) {
- System.out.println(s + " Got an error! " + errs);
- }
- add(time, count, s);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- close();
- }
- }
-
- void send(int percentage) {
- try {
- s.getOutputStream().write((percentage + "\n").getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- void close() {
- try {
- System.err.println("Closing " + s);
- slaves.remove(this);
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- static class AcceptorThread extends Thread {
- AcceptorThread() {
- setDaemon(true);
- start();
- }
-
- public void run() {
- try {
- while (true) {
- Socket s = ss.accept();
- System.err.println("Accepted connection from " + s);
- slaves.add(new SlaveThread(s));
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- for (Iterator<SlaveThread> it = slaves.iterator(); it.hasNext();) {
- SlaveThread st = it.next();
- it.remove();
- st.close();
- }
- }
- }
- }
-
- static class ReporterThread extends Thread {
- static int percentage;
-
- ReporterThread() {
- setDaemon(true);
- start();
- }
-
- public void run() {
- try {
- currentInterval = Time.currentElapsedTime() / INTERVAL;
- // Give things time to report;
- Thread.sleep(INTERVAL * 2);
- long min = 99999;
- long max = 0;
- long total = 0;
- int number = 0;
- while (true) {
- long now = Time.currentElapsedTime();
- long lastInterval = currentInterval;
- currentInterval += 1;
- long count = remove(lastInterval);
- count = count * 1000 / INTERVAL; // Multiply by 1000 to get
- // reqs/sec
- if (lastChange != 0
- && (lastChange + INTERVAL * 3) < now) {
- // We only want to print anything if things have had a
- // chance to change
-
- if (count < min) {
- min = count;
- }
- if (count > max) {
- max = count;
- }
- total += count;
- number++;
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(lastInterval * INTERVAL);
- String report = lastInterval + " "
- + calendar.get(Calendar.HOUR_OF_DAY) + ":"
- + calendar.get(Calendar.MINUTE) + ":"
- + calendar.get(Calendar.SECOND) + " "
- + percentage + "% " + count + " " + min + " "
- + ((double) total / (double) number) + " "
- + max;
- System.err.println(report);
- if (sf != null) {
- sf.println(report);
- }
- } else {
- max = total = 0;
- min = 999999999;
- number = 0;
- }
- Thread.sleep(INTERVAL);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- }
-
- synchronized static void sendChange(int percentage) {
- long now = Time.currentElapsedTime();
- long start = now;
- ReporterThread.percentage = percentage;
- for (SlaveThread st : slaves.toArray(new SlaveThread[0])) {
- st.send(percentage);
- }
- now = Time.currentElapsedTime();
- long delay = now - start;
- if (delay > 1000) {
- System.out.println("Delay of " + delay + " to send new percentage");
- }
- lastChange = now;
- }
-
- static public class GeneratorInstance implements Instance {
-
- byte bytes[];
-
- int percentage = -1;
-
- int errors;
-
- final Object statSync = new Object();
-
- int finished;
-
- int reads;
-
- int writes;
-
- int rlatency;
-
- int wlatency;
-
- int outstanding;
-
- volatile boolean alive;
-
- class ZooKeeperThread extends Thread implements Watcher, DataCallback,
- StatCallback {
- String host;
-
- ZooKeeperThread(String host) {
- setDaemon(true);
- alive = true;
- this.host = host;
- start();
- }
-
- static final int outstandingLimit = 100;
-
- synchronized void incOutstanding() throws InterruptedException {
- outstanding++;
- while (outstanding > outstandingLimit) {
- wait();
- }
- }
-
- synchronized void decOutstanding() {
- outstanding--;
- notifyAll();
- }
-
- Random r = new Random();
-
- String path;
-
- ZooKeeper zk;
-
- boolean connected;
-
- public void run() {
- try {
- zk = new ZooKeeper(host, 60000, this);
- synchronized (this) {
- if (!connected) {
- wait(20000);
- }
- }
- for (int i = 0; i < 300; i++) {
- try {
- Thread.sleep(100);
- path = zk.create("/client", new byte[16],
- Ids.OPEN_ACL_UNSAFE,
- CreateMode.EPHEMERAL_SEQUENTIAL);
- break;
- } catch (KeeperException e) {
- LOG.error("keeper exception thrown", e);
- }
- }
- if (path == null) {
- System.err.println("Couldn't create a node in /!");
- return;
- }
- while (alive) {
- if (r.nextInt(100) < percentage) {
- zk.setData(path, bytes, -1, this, System
- .currentTimeMillis());
- } else {
- zk.getData(path, false, this, System
- .currentTimeMillis());
- }
- incOutstanding();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- alive = false;
- try {
- zk.close();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- public void process(WatchedEvent event) {
- System.err.println(event);
- synchronized (this) {
- if (event.getType() == EventType.None) {
- connected = (event.getState() == KeeperState.SyncConnected);
- notifyAll();
- }
- }
- }
-
- public void processResult(int rc, String path, Object ctx, byte[] data,
- Stat stat) {
- decOutstanding();
- synchronized (statSync) {
- if (!alive) {
- return;
- }
- if (rc != 0) {
- System.err.println("Got rc = " + rc);
- errors++;
- } else {
- finished++;
- rlatency += Time.currentElapsedTime() - (Long) ctx;
- reads++;
- }
- }
- }
-
- public void processResult(int rc, String path, Object ctx, Stat stat) {
- decOutstanding();
- synchronized (statSync) {
- if (rc != 0) {
- System.err.println("Got rc = " + rc);
- errors++;
- } else {
- finished++;
- wlatency += Time.currentElapsedTime() - (Long) ctx;
- writes++;
- }
- }
- }
- }
-
- class SenderThread extends Thread {
- Socket s;
-
- SenderThread(Socket s) {
- this.s = s;
- setDaemon(true);
- start();
- }
-
- public void run() {
- try {
- OutputStream os = s.getOutputStream();
- finished = 0;
- errors = 0;
- while (alive) {
- Thread.sleep(300);
- if (percentage == -1 || (finished == 0 && errors == 0)) {
- continue;
- }
- String report = Time.currentElapsedTime() + " "
- + percentage + " " + finished + " " + errors + " "
- + outstanding + "\n";
- /* String subreport = reads + " "
- + (((double) rlatency) / reads) + " " + writes
- + " " + (((double) wlatency / writes)); */
- synchronized (statSync) {
- finished = 0;
- errors = 0;
- reads = 0;
- writes = 0;
- rlatency = 0;
- wlatency = 0;
- }
- os.write(report.getBytes());
- //System.out.println("Reporting " + report + "+" + subreport);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- }
-
- Socket s;
- ZooKeeperThread zkThread;
- SenderThread sendThread;
- Reporter r;
-
- public void configure(final String params) {
- System.err.println("Got " + params);
- new Thread() {
- public void run() {
- try {
- String parts[] = params.split(" ");
- String hostPort[] = parts[1].split(":");
- int bytesSize = 1024;
- if (parts.length == 3) {
- try {
- bytesSize = Integer.parseInt(parts[2]);
- } catch(Exception e) {
- System.err.println("Not an integer: " + parts[2]);
- }
- }
- bytes = new byte[bytesSize];
- s = new Socket(hostPort[0], Integer.parseInt(hostPort[1]));
- zkThread = new ZooKeeperThread(parts[0]);
- sendThread = new SenderThread(s);
- BufferedReader is = new BufferedReader(new InputStreamReader(s
- .getInputStream()));
- String line;
- while ((line = is.readLine()) != null) {
- percentage = Integer.parseInt(line);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }.start();
-
- }
-
- public void setReporter(Reporter r) {
- this.r = r;
- }
-
- public void start() {
- try {
- r.report("started");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public void stop() {
- alive = false;
- zkThread.interrupt();
- sendThread.interrupt();
- try {
- zkThread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- try {
- sendThread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- try {
- r.report("stopped");
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
-
- private static class StatusWatcher implements Watcher {
- volatile boolean connected;
-
- public void process(WatchedEvent event) {
- if (event.getType() == Watcher.Event.EventType.None) {
- synchronized (this) {
- connected = event.getState() == Watcher.Event.KeeperState.SyncConnected;
- notifyAll();
- }
- }
- }
-
- synchronized public boolean waitConnected(long timeout)
- throws InterruptedException {
- long endTime = Time.currentElapsedTime() + timeout;
- while (!connected && Time.currentElapsedTime() < endTime) {
- wait(endTime - Time.currentElapsedTime());
- }
- return connected;
- }
- }
-
- private static boolean leaderOnly;
- private static boolean leaderServes;
-
- private static String []processOptions(String args[]) {
- ArrayList<String> newArgs = new ArrayList<String>();
- for(String a: args) {
- if (a.equals("--leaderOnly")) {
- leaderOnly = true;
- leaderServes = true;
- } else if (a.equals("--leaderServes")) {
- leaderServes = true;
- } else {
- newArgs.add(a);
- }
- }
- return newArgs.toArray(new String[0]);
- }
-
- /**
- * @param args
- * @throws InterruptedException
- * @throws KeeperException
- * @throws DuplicateNameException
- * @throws NoAvailableContainers
- * @throws NoAssignmentException
- */
- public static void main(String[] args) throws InterruptedException,
- KeeperException, NoAvailableContainers, DuplicateNameException,
- NoAssignmentException {
-
- args = processOptions(args);
- if (args.length == 5) {
- try {
- StatusWatcher statusWatcher = new StatusWatcher();
- ZooKeeper zk = new ZooKeeper(args[0], 15000, statusWatcher);
- if (!statusWatcher.waitConnected(5000)) {
- System.err.println("Could not connect to " + args[0]);
- return;
- }
- InstanceManager im = new InstanceManager(zk, args[1]);
- ss = new ServerSocket(0);
- int port = ss.getLocalPort();
- int serverCount = Integer.parseInt(args[2]);
- int clientCount = Integer.parseInt(args[3]);
- StringBuilder quorumHostPort = new StringBuilder();
- StringBuilder zkHostPort = new StringBuilder();
- for (int i = 0; i < serverCount; i++) {
- String r[] = QuorumPeerInstance.createServer(im, i, leaderServes);
- if (i > 0) {
- quorumHostPort.append(',');
- zkHostPort.append(',');
- }
- zkHostPort.append(r[0]); // r[0] == "host:clientPort"
- quorumHostPort.append(r[1]); // r[1] == "host:leaderPort:leaderElectionPort"
- quorumHostPort.append(";"+(r[0].split(":"))[1]); // Appending ";clientPort"
- }
- for (int i = 0; i < serverCount; i++) {
- QuorumPeerInstance.startInstance(im, quorumHostPort
- .toString(), i);
- }
- if (leaderOnly) {
- int tries = 0;
- outer:
- while(true) {
- Thread.sleep(1000);
- IOException lastException = null;
- String parts[] = zkHostPort.toString().split(",");
- for(int i = 0; i < parts.length; i++) {
- try {
- String mode = getMode(parts[i]);
- if (mode.equals("leader")) {
- zkHostPort = new StringBuilder(parts[i]);
- System.out.println("Connecting exclusively to " + zkHostPort.toString());
- break outer;
- }
- } catch(IOException e) {
- lastException = e;
- }
- }
- if (tries++ > 3) {
- throw lastException;
- }
- }
- }
- for (int i = 0; i < clientCount; i++) {
- im.assignInstance("client" + i, GeneratorInstance.class,
- zkHostPort.toString()
- + ' '
- + InetAddress.getLocalHost()
- .getCanonicalHostName() + ':'
- + port, 1);
- }
- new AcceptorThread();
- new ReporterThread();
- BufferedReader is = new BufferedReader(new InputStreamReader(
- System.in));
- String line;
- while ((line = is.readLine()) != null) {
- try {
- String cmdNumber[] = line.split(" ");
- if (cmdNumber[0].equals("percentage")
- && cmdNumber.length > 1) {
- int number = Integer.parseInt(cmdNumber[1]);
- if (number < 0 || number > 100) {
- throw new NumberFormatException(
- "must be between 0 and 100");
- }
- sendChange(number);
- } else if (cmdNumber[0].equals("sleep")
- && cmdNumber.length > 1) {
- int number = Integer.parseInt(cmdNumber[1]);
- Thread.sleep(number * 1000);
- } else if (cmdNumber[0].equals("save")
- && cmdNumber.length > 1) {
- sf = new PrintStream(cmdNumber[1]);
- } else {
- System.err.println("Commands must be:");
- System.err
- .println("\tpercentage new_write_percentage");
- System.err.println("\tsleep seconds_to_sleep");
- System.err.println("\tsave file_to_save_output");
- }
- } catch (NumberFormatException e) {
- System.out.println("Not a valid number: "
- + e.getMessage());
- }
- }
- } catch (NumberFormatException e) {
- doUsage();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(2);
- }
- } else {
- doUsage();
- }
-
- }
-
- private static String getMode(String hostPort) throws NumberFormatException, UnknownHostException, IOException {
- String parts[] = hostPort.split(":");
- Socket s = new Socket(parts[0], Integer.parseInt(parts[1]));
- s.getOutputStream().write("stat".getBytes());
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String line;
- try {
- while((line = br.readLine()) != null) {
- if (line.startsWith("Mode: ")) {
- return line.substring(6);
- }
- }
- return "unknown";
- } finally {
- s.close();
- }
- }
-
- private static void doUsage() {
- System.err.println("USAGE: " + GenerateLoad.class.getName()
- + " [--leaderOnly] [--leaderServes] zookeeper_host:port containerPrefix #ofServers #ofClients requestSize");
- System.exit(2);
- }
-}
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/systest/org/apache/zookeeper/test/system/Instance.java
----------------------------------------------------------------------
diff --git a/src/java/systest/org/apache/zookeeper/test/system/Instance.java b/src/java/systest/org/apache/zookeeper/test/system/Instance.java
deleted file mode 100644
index 9b980de..0000000
--- a/src/java/systest/org/apache/zookeeper/test/system/Instance.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
- * 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.zookeeper.test.system;
-
-import org.apache.zookeeper.KeeperException;
-
-/**
- * This interface is implemented by a class that can be run in an
- * instance container.
- *
- */
-public interface Instance {
- /**
- * This object is used to report back changes in status.
- */
- interface Reporter {
- void report(String report) throws KeeperException, InterruptedException;
- }
- /**
- * This will be the first method invoked by the InstanceContainer after
- * an instance of this interface has been constructed. It will only be
- * invoked once.
- *
- * @param r a handle to use to report on status changes.
- */
- void setReporter(Reporter r);
- /**
- * This will be the second method invoked by the InstanceContainer. It
- * may be invoked again if the configuration changes.
- *
- * @param params parameters that were passed to the InstanceManager when
- * this instance was scheduled.
- */
- void configure(String params);
- /**
- * Starts this instance.
- */
- void start();
- /**
- * Stops this instance.
- */
- void stop();
-}