Return-Path: X-Original-To: apmail-accumulo-commits-archive@www.apache.org Delivered-To: apmail-accumulo-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D71D0177BF for ; Sat, 1 Nov 2014 04:57:02 +0000 (UTC) Received: (qmail 83999 invoked by uid 500); 1 Nov 2014 04:57:02 -0000 Delivered-To: apmail-accumulo-commits-archive@accumulo.apache.org Received: (qmail 83968 invoked by uid 500); 1 Nov 2014 04:57:02 -0000 Mailing-List: contact commits-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list commits@accumulo.apache.org Received: (qmail 83959 invoked by uid 99); 1 Nov 2014 04:57:02 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 01 Nov 2014 04:57:02 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 5621A993661; Sat, 1 Nov 2014 04:57:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ctubbsii@apache.org To: commits@accumulo.apache.org Date: Sat, 01 Nov 2014 04:57:26 -0000 Message-Id: <42c84cdd265b492f966cfa6de14cd05b@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [32/50] [abbrv] Merge branch '1.5' into 1.6 http://git-wip-us.apache.org/repos/asf/accumulo/blob/9b20a9d4/server/base/src/main/java/org/apache/accumulo/server/watcher/MonitorLog4jWatcher.java ---------------------------------------------------------------------- diff --cc server/base/src/main/java/org/apache/accumulo/server/watcher/MonitorLog4jWatcher.java index 292129d,0000000..7ae4d5a mode 100644,000000..100644 --- a/server/base/src/main/java/org/apache/accumulo/server/watcher/MonitorLog4jWatcher.java +++ b/server/base/src/main/java/org/apache/accumulo/server/watcher/MonitorLog4jWatcher.java @@@ -1,150 -1,0 +1,152 @@@ +/* + * 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.accumulo.server.watcher; + ++import static com.google.common.base.Charsets.UTF_8; ++ +import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.zookeeper.ZooUtil; +import org.apache.accumulo.server.zookeeper.ZooReaderWriter; +import org.apache.log4j.Appender; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.apache.log4j.helpers.FileWatchdog; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; + +import com.google.common.net.HostAndPort; + +/** + * Watcher that updates the monitor's log4j port from ZooKeeper in a system property + */ +public class MonitorLog4jWatcher extends FileWatchdog implements Watcher { + private static final Logger log = Logger.getLogger(MonitorLog4jWatcher.class); + + private static final String HOST_PROPERTY_NAME = "org.apache.accumulo.core.host.log"; + private static final String PORT_PROPERTY_NAME = "org.apache.accumulo.core.host.log.port"; + + private final Object lock; + private final Log4jConfiguration logConfig; + private boolean loggingDisabled = false; + protected String path; + + public MonitorLog4jWatcher(String instance, String filename) { + super(filename); + this.path = ZooUtil.getRoot(instance) + Constants.ZMONITOR_LOG4J_ADDR; + this.lock = new Object(); + this.logConfig = new Log4jConfiguration(filename); + doOnChange(); + } + + boolean isUsingProperties() { + return logConfig.isUsingProperties(); + } + + String getPath() { + return path; + } + + @Override + public void run() { + try { + // Initially set the logger if the Monitor's log4j advertisement node exists + if (ZooReaderWriter.getInstance().exists(path, this)) + updateMonitorLog4jLocation(); + log.info("Set watch for Monitor Log4j watcher"); + } catch (Exception e) { + log.error("Unable to set watch for Monitor Log4j watcher on " + path); + } + + super.run(); + } + + @Override + public void doOnChange() { + // this method gets called in the parent class' constructor + // I'm not sure of a better way to get around this. The final modifier helps though. + if (null == lock) { + return; + } + + synchronized (lock) { + // We might triggered by file-reloading or from ZK update. + // Either way will result in log-forwarding being restarted + loggingDisabled = false; + log.info("Enabled log-forwarding"); + logConfig.resetLogger(); + } + } + + @Override + public void process(WatchedEvent event) { + // We got an update, process the data in the node + updateMonitorLog4jLocation(); + + if (event.getPath() != null) { + try { + ZooReaderWriter.getInstance().exists(event.getPath(), this); + } catch (Exception ex) { + log.error("Unable to reset watch for Monitor Log4j watcher", ex); + } + } + } + + /** + * Read the host and port information for the Monitor's log4j socket and update the system properties so that, on logger refresh, it sees the new information. + */ + protected void updateMonitorLog4jLocation() { + try { - String hostPortString = new String(ZooReaderWriter.getInstance().getData(path, null), Constants.UTF8); ++ String hostPortString = new String(ZooReaderWriter.getInstance().getData(path, null), UTF_8); + HostAndPort hostAndPort = HostAndPort.fromString(hostPortString); + + System.setProperty(HOST_PROPERTY_NAME, hostAndPort.getHostText()); + System.setProperty(PORT_PROPERTY_NAME, Integer.toString(hostAndPort.getPort())); + + log.info("Changing monitor log4j address to " + hostAndPort.toString()); + + doOnChange(); + } catch (NoNodeException e) { + // Not sure on the synchronization guarantees for Loggers and Appenders + // on configuration reload + synchronized (lock) { + // Don't need to try to re-disable'ing it. + if (loggingDisabled) { + return; + } + + Logger logger = LogManager.getLogger("org.apache.accumulo"); + if (null != logger) { + // TODO ACCUMULO-2343 Create a specific appender for log-forwarding to the monitor + // that can replace the AsyncAppender+SocketAppender. + Appender appender = logger.getAppender("ASYNC"); + if (null != appender) { + log.info("Closing log-forwarding appender"); + appender.close(); + log.info("Removing log-forwarding appender"); + logger.removeAppender(appender); + loggingDisabled = true; + } + } + } + } catch (IllegalArgumentException e) { + log.error("Could not parse host and port information", e); + } catch (Exception e) { + log.error("Error reading zookeeper data for Monitor Log4j watcher", e); + } + } +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/9b20a9d4/server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooQueueLock.java ---------------------------------------------------------------------- diff --cc server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooQueueLock.java index b02e5d4,0000000..1bbae03 mode 100644,000000..100644 --- a/server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooQueueLock.java +++ b/server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooQueueLock.java @@@ -1,54 -1,0 +1,55 @@@ +/* + * 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.accumulo.server.zookeeper; + ++import static com.google.common.base.Charsets.UTF_8; ++ +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; + - import org.apache.accumulo.core.Constants; +import org.apache.accumulo.fate.zookeeper.DistributedReadWriteLock; +import org.apache.zookeeper.KeeperException; + +public class ZooQueueLock extends org.apache.accumulo.fate.zookeeper.ZooQueueLock { + + public ZooQueueLock(String path, boolean ephemeral) throws KeeperException, InterruptedException { + super(ZooReaderWriter.getInstance(), path, ephemeral); + } + + public static void main(String args[]) throws InterruptedException, KeeperException { + ZooQueueLock lock = new ZooQueueLock("/lock", true); - DistributedReadWriteLock rlocker = new DistributedReadWriteLock(lock, "reader".getBytes(Constants.UTF8)); - DistributedReadWriteLock wlocker = new DistributedReadWriteLock(lock, "wlocker".getBytes(Constants.UTF8)); ++ DistributedReadWriteLock rlocker = new DistributedReadWriteLock(lock, "reader".getBytes(UTF_8)); ++ DistributedReadWriteLock wlocker = new DistributedReadWriteLock(lock, "wlocker".getBytes(UTF_8)); + final Lock readLock = rlocker.readLock(); + readLock.lock(); + final Lock readLock2 = rlocker.readLock(); + readLock2.lock(); + final Lock writeLock = wlocker.writeLock(); + if (writeLock.tryLock(100, TimeUnit.MILLISECONDS)) + throw new RuntimeException("Write lock achieved during read lock!"); + readLock.unlock(); + readLock2.unlock(); + writeLock.lock(); + if (readLock.tryLock(100, TimeUnit.MILLISECONDS)) + throw new RuntimeException("Read lock achieved during write lock!"); - final Lock writeLock2 = DistributedReadWriteLock.recoverLock(lock, "wlocker".getBytes(Constants.UTF8)); ++ final Lock writeLock2 = DistributedReadWriteLock.recoverLock(lock, "wlocker".getBytes(UTF_8)); + writeLock2.unlock(); + readLock.lock(); + System.out.println("success"); + } + +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/9b20a9d4/server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooReaderWriter.java ---------------------------------------------------------------------- diff --cc server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooReaderWriter.java index 435591d,0000000..c5374a2 mode 100644,000000..100644 --- a/server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooReaderWriter.java +++ b/server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooReaderWriter.java @@@ -1,42 -1,0 +1,43 @@@ +/* + * 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.accumulo.server.zookeeper; + - import org.apache.accumulo.core.Constants; ++import static com.google.common.base.Charsets.UTF_8; ++ +import org.apache.accumulo.core.conf.AccumuloConfiguration; +import org.apache.accumulo.core.conf.Property; +import org.apache.accumulo.server.conf.ServerConfiguration; + +public class ZooReaderWriter extends org.apache.accumulo.fate.zookeeper.ZooReaderWriter { + private static final String SCHEME = "digest"; + private static final String USER = "accumulo"; + private static ZooReaderWriter instance = null; + + public ZooReaderWriter(String string, int timeInMillis, String secret) { - super(string, timeInMillis, SCHEME, (USER + ":" + secret).getBytes(Constants.UTF8)); ++ super(string, timeInMillis, SCHEME, (USER + ":" + secret).getBytes(UTF_8)); + } + + public static synchronized ZooReaderWriter getInstance() { + if (instance == null) { + AccumuloConfiguration conf = ServerConfiguration.getSiteConfiguration(); + instance = new ZooReaderWriter(conf.get(Property.INSTANCE_ZK_HOST), (int) conf.getTimeInMillis(Property.INSTANCE_ZK_TIMEOUT), + conf.get(Property.INSTANCE_SECRET)); + } + return instance; + } + +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/9b20a9d4/server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooReaderWriterFactory.java ---------------------------------------------------------------------- diff --cc server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooReaderWriterFactory.java index 4a7111d,0000000..e15e63e mode 100644,000000..100644 --- a/server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooReaderWriterFactory.java +++ b/server/base/src/main/java/org/apache/accumulo/server/zookeeper/ZooReaderWriterFactory.java @@@ -1,64 -1,0 +1,65 @@@ +/* + * 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.accumulo.server.zookeeper; + - import org.apache.accumulo.core.Constants; ++import static com.google.common.base.Charsets.UTF_8; ++ +import org.apache.accumulo.core.conf.AccumuloConfiguration; +import org.apache.accumulo.core.conf.Property; +import org.apache.accumulo.fate.zookeeper.IZooReaderWriter; +import org.apache.accumulo.fate.zookeeper.ZooReaderWriter; +import org.apache.accumulo.server.conf.ServerConfiguration; + +/** + * A factory for {@link ZooReaderWriter} objects. + */ +public class ZooReaderWriterFactory { + private static final String SCHEME = "digest"; + private static final String USER = "accumulo"; + private static IZooReaderWriter instance = null; + + /** + * Gets a new reader/writer. + * + * @param string + * ZooKeeper connection string + * @param timeInMillis + * session timeout in milliseconds + * @param secret + * instance secret + * @return reader/writer + */ + public IZooReaderWriter getZooReaderWriter(String string, int timeInMillis, String secret) { - return new ZooReaderWriter(string, timeInMillis, SCHEME, (USER + ":" + secret).getBytes(Constants.UTF8)); ++ return new ZooReaderWriter(string, timeInMillis, SCHEME, (USER + ":" + secret).getBytes(UTF_8)); + } + + /** + * Gets a reader/writer, retrieving ZooKeeper information from the site configuration. The same instance may be returned for multiple calls. + * + * @return reader/writer + */ + public IZooReaderWriter getInstance() { + synchronized (ZooReaderWriterFactory.class) { + if (instance == null) { + AccumuloConfiguration conf = ServerConfiguration.getSiteConfiguration(); + instance = getZooReaderWriter(conf.get(Property.INSTANCE_ZK_HOST), (int) conf.getTimeInMillis(Property.INSTANCE_ZK_TIMEOUT), + conf.get(Property.INSTANCE_SECRET)); + } + return instance; + } + } +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/9b20a9d4/server/base/src/test/java/org/apache/accumulo/server/conf/NamespaceConfigurationTest.java ---------------------------------------------------------------------- diff --cc server/base/src/test/java/org/apache/accumulo/server/conf/NamespaceConfigurationTest.java index e7f42af,0000000..3aed7ef mode 100644,000000..100644 --- a/server/base/src/test/java/org/apache/accumulo/server/conf/NamespaceConfigurationTest.java +++ b/server/base/src/test/java/org/apache/accumulo/server/conf/NamespaceConfigurationTest.java @@@ -1,159 -1,0 +1,161 @@@ +/* + * 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.accumulo.server.conf; + ++import static com.google.common.base.Charsets.UTF_8; ++ +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.client.Instance; +import org.apache.accumulo.core.client.impl.Namespaces; +import org.apache.accumulo.core.conf.AccumuloConfiguration; +import org.apache.accumulo.core.conf.AccumuloConfiguration.AllFilter; +import org.apache.accumulo.core.conf.AccumuloConfiguration.PropertyFilter; +import org.apache.accumulo.core.conf.ConfigurationObserver; +import org.apache.accumulo.core.conf.Property; +import org.apache.accumulo.core.zookeeper.ZooUtil; +import org.apache.accumulo.fate.zookeeper.ZooCache; +import org.apache.accumulo.fate.zookeeper.ZooCacheFactory; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +public class NamespaceConfigurationTest { + private static final String NSID = "namespace"; + private static final String ZOOKEEPERS = "localhost"; + private static final int ZK_SESSION_TIMEOUT = 120000; + + private String iid; + private Instance instance; + private AccumuloConfiguration parent; + private ZooCacheFactory zcf; + private ZooCache zc; + private NamespaceConfiguration c; + + @Before + public void setUp() { + iid = UUID.randomUUID().toString(); + instance = createMock(Instance.class); + parent = createMock(AccumuloConfiguration.class); + c = new NamespaceConfiguration(NSID, instance, parent); + zcf = createMock(ZooCacheFactory.class); + c.setZooCacheFactory(zcf); + + expect(instance.getInstanceID()).andReturn(iid); + expectLastCall().anyTimes(); + expect(instance.getZooKeepers()).andReturn(ZOOKEEPERS); + expect(instance.getZooKeepersSessionTimeOut()).andReturn(ZK_SESSION_TIMEOUT); + replay(instance); + zc = createMock(ZooCache.class); + expect(zcf.getZooCache(eq(ZOOKEEPERS), eq(ZK_SESSION_TIMEOUT), anyObject(NamespaceConfWatcher.class))).andReturn(zc); + replay(zcf); + } + + @Test + public void testGetters() { + assertEquals(NSID, c.getNamespaceId()); + assertEquals(parent, c.getParentConfiguration()); + } + + @Test + public void testGet_InZK() { + Property p = Property.INSTANCE_SECRET; + expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZNAMESPACES + "/" + NSID + Constants.ZNAMESPACE_CONF + "/" + p.getKey())).andReturn( - "sekrit".getBytes(Constants.UTF8)); ++ "sekrit".getBytes(UTF_8)); + replay(zc); + assertEquals("sekrit", c.get(Property.INSTANCE_SECRET)); + } + + @Test + public void testGet_InParent() { + Property p = Property.INSTANCE_SECRET; + expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZNAMESPACES + "/" + NSID + Constants.ZNAMESPACE_CONF + "/" + p.getKey())).andReturn(null); + replay(zc); + expect(parent.get(p)).andReturn("sekrit"); + replay(parent); + assertEquals("sekrit", c.get(Property.INSTANCE_SECRET)); + } + + @Test + public void testGet_SkipParentIfAccumuloNS() { + c = new NamespaceConfiguration(Namespaces.ACCUMULO_NAMESPACE_ID, instance, parent); + c.setZooCacheFactory(zcf); + Property p = Property.INSTANCE_SECRET; + expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZNAMESPACES + "/" + Namespaces.ACCUMULO_NAMESPACE_ID + Constants.ZNAMESPACE_CONF + "/" + p.getKey())) + .andReturn(null); + replay(zc); + assertNull(c.get(Property.INSTANCE_SECRET)); + } + + @Test + public void testGetProperties() { + PropertyFilter filter = new AllFilter(); + Map props = new java.util.HashMap(); + parent.getProperties(props, filter); + replay(parent); + List children = new java.util.ArrayList(); + children.add("foo"); + children.add("ding"); + expect(zc.getChildren(ZooUtil.getRoot(iid) + Constants.ZNAMESPACES + "/" + NSID + Constants.ZNAMESPACE_CONF)).andReturn(children); + expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZNAMESPACES + "/" + NSID + Constants.ZNAMESPACE_CONF + "/" + "foo")).andReturn( - "bar".getBytes(Constants.UTF8)); ++ "bar".getBytes(UTF_8)); + expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZNAMESPACES + "/" + NSID + Constants.ZNAMESPACE_CONF + "/" + "ding")).andReturn( - "dong".getBytes(Constants.UTF8)); ++ "dong".getBytes(UTF_8)); + replay(zc); + c.getProperties(props, filter); + assertEquals(2, props.size()); + assertEquals("bar", props.get("foo")); + assertEquals("dong", props.get("ding")); + } + + @Test + public void testObserver() { + ConfigurationObserver o = createMock(ConfigurationObserver.class); + c.addObserver(o); + Collection os = c.getObservers(); + assertEquals(1, os.size()); + assertTrue(os.contains(o)); + c.removeObserver(o); + os = c.getObservers(); + assertEquals(0, os.size()); + } + + @Test + public void testInvalidateCache() { + // need to do a get so the accessor is created + Property p = Property.INSTANCE_SECRET; + expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZNAMESPACES + "/" + NSID + Constants.ZNAMESPACE_CONF + "/" + p.getKey())).andReturn( - "sekrit".getBytes(Constants.UTF8)); ++ "sekrit".getBytes(UTF_8)); + zc.clear(); + replay(zc); + c.get(Property.INSTANCE_SECRET); + c.invalidateCache(); + verify(zc); + } +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/9b20a9d4/server/base/src/test/java/org/apache/accumulo/server/conf/ServerConfigurationFactoryTest.java ---------------------------------------------------------------------- diff --cc server/base/src/test/java/org/apache/accumulo/server/conf/ServerConfigurationFactoryTest.java index 7f07d94,0000000..81148a0 mode 100644,000000..100644 --- a/server/base/src/test/java/org/apache/accumulo/server/conf/ServerConfigurationFactoryTest.java +++ b/server/base/src/test/java/org/apache/accumulo/server/conf/ServerConfigurationFactoryTest.java @@@ -1,134 -1,0 +1,134 @@@ +/* + * 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.accumulo.server.conf; + ++import static com.google.common.base.Charsets.UTF_8; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.endsWith; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; + - import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.client.Instance; +import org.apache.accumulo.core.conf.AccumuloConfiguration; +import org.apache.accumulo.core.conf.DefaultConfiguration; +import org.apache.accumulo.core.conf.SiteConfiguration; +import org.apache.accumulo.fate.zookeeper.ZooCache; +import org.apache.accumulo.fate.zookeeper.ZooCacheFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ServerConfigurationFactoryTest { + private static final String ZK_HOST = "localhost"; + private static final int ZK_TIMEOUT = 120000; + private static final String IID = "iid"; + + // use the same mock ZooCacheFactory and ZooCache for all tests + private static ZooCacheFactory zcf; + private static ZooCache zc; + + @BeforeClass + public static void setUpClass() throws Exception { + zcf = createMock(ZooCacheFactory.class); + zc = createMock(ZooCache.class); + expect(zcf.getZooCache(eq(ZK_HOST), eq(ZK_TIMEOUT), anyObject(NamespaceConfWatcher.class))).andReturn(zc); + expectLastCall().anyTimes(); + expect(zcf.getZooCache(ZK_HOST, ZK_TIMEOUT)).andReturn(zc); + expectLastCall().anyTimes(); + replay(zcf); + + expect(zc.getChildren(anyObject(String.class))).andReturn(null); + expectLastCall().anyTimes(); + // ConfigSanityCheck looks at timeout - expect(zc.get(endsWith("timeout"))).andReturn(("" + ZK_TIMEOUT + "ms").getBytes(Constants.UTF8)); ++ expect(zc.get(endsWith("timeout"))).andReturn(("" + ZK_TIMEOUT + "ms").getBytes(UTF_8)); + replay(zc); + } + + private Instance instance; + private ServerConfigurationFactory scf; + + @Before + public void setUp() throws Exception { + instance = createMock(Instance.class); + expect(instance.getInstanceID()).andReturn(IID); + expectLastCall().anyTimes(); + } + + @After + public void tearDown() throws Exception { + ServerConfigurationFactory.clearCachedConfigurations(); + } + + private void mockInstanceForConfig() { + expect(instance.getZooKeepers()).andReturn(ZK_HOST); + expect(instance.getZooKeepersSessionTimeOut()).andReturn(ZK_TIMEOUT); + } + + private void ready() { + replay(instance); + scf = new ServerConfigurationFactory(instance); + scf.setZooCacheFactory(zcf); + } + + @Test + public void testGetInstance() { + ready(); + assertSame(instance, scf.getInstance()); + } + + @Test + public void testGetDefaultConfiguration() { + ready(); + DefaultConfiguration c = scf.getDefaultConfiguration(); + assertNotNull(c); + } + + @Test + public void testGetSiteConfiguration() { + ready(); + SiteConfiguration c = scf.getSiteConfiguration(); + assertNotNull(c); + } + + @Test + public void testGetConfiguration() { + mockInstanceForConfig(); + ready(); + AccumuloConfiguration c = scf.getConfiguration(); + assertNotNull(c); + } + + private static final String NSID = "NAMESPACE"; + + @Test + public void testGetNamespaceConfiguration() { + mockInstanceForConfig(); + ready(); + NamespaceConfiguration c = scf.getNamespaceConfiguration(NSID); + assertEquals(NSID, c.getNamespaceId()); + + assertSame(c, scf.getNamespaceConfiguration(NSID)); + } + +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/9b20a9d4/server/base/src/test/java/org/apache/accumulo/server/conf/TableConfigurationTest.java ---------------------------------------------------------------------- diff --cc server/base/src/test/java/org/apache/accumulo/server/conf/TableConfigurationTest.java index 92580f3,0000000..2f38e4c mode 100644,000000..100644 --- a/server/base/src/test/java/org/apache/accumulo/server/conf/TableConfigurationTest.java +++ b/server/base/src/test/java/org/apache/accumulo/server/conf/TableConfigurationTest.java @@@ -1,145 -1,0 +1,146 @@@ +/* + * 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.accumulo.server.conf; + ++import static com.google.common.base.Charsets.UTF_8; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.client.Instance; +import org.apache.accumulo.core.conf.AccumuloConfiguration.AllFilter; +import org.apache.accumulo.core.conf.AccumuloConfiguration.PropertyFilter; +import org.apache.accumulo.core.conf.ConfigurationObserver; +import org.apache.accumulo.core.conf.Property; +import org.apache.accumulo.core.zookeeper.ZooUtil; +import org.apache.accumulo.fate.zookeeper.ZooCache; +import org.apache.accumulo.fate.zookeeper.ZooCacheFactory; +import org.junit.Before; +import org.junit.Test; + +public class TableConfigurationTest { + private static final String TID = "table"; + private static final String ZOOKEEPERS = "localhost"; + private static final int ZK_SESSION_TIMEOUT = 120000; + + private String iid; + private Instance instance; + private NamespaceConfiguration parent; + private ZooCacheFactory zcf; + private ZooCache zc; + private TableConfiguration c; + + @Before + public void setUp() { + iid = UUID.randomUUID().toString(); + instance = createMock(Instance.class); + parent = createMock(NamespaceConfiguration.class); + c = new TableConfiguration(iid, instance, TID, parent); + zcf = createMock(ZooCacheFactory.class); + c.setZooCacheFactory(zcf); + + expect(instance.getInstanceID()).andReturn(iid); + expectLastCall().anyTimes(); + expect(instance.getZooKeepers()).andReturn(ZOOKEEPERS); + expect(instance.getZooKeepersSessionTimeOut()).andReturn(ZK_SESSION_TIMEOUT); + replay(instance); + zc = createMock(ZooCache.class); + expect(zcf.getZooCache(eq(ZOOKEEPERS), eq(ZK_SESSION_TIMEOUT), anyObject(TableConfWatcher.class))).andReturn(zc); + replay(zcf); + } + + @Test + public void testGetters() { + assertEquals(TID, c.getTableId()); + assertEquals(parent, c.getParentConfiguration()); + } + + @Test + public void testGet_InZK() { + Property p = Property.INSTANCE_SECRET; + expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZTABLES + "/" + TID + Constants.ZTABLE_CONF + "/" + p.getKey())) - .andReturn("sekrit".getBytes(Constants.UTF8)); ++ .andReturn("sekrit".getBytes(UTF_8)); + replay(zc); + assertEquals("sekrit", c.get(Property.INSTANCE_SECRET)); + } + + @Test + public void testGet_InParent() { + Property p = Property.INSTANCE_SECRET; + expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZTABLES + "/" + TID + Constants.ZTABLE_CONF + "/" + p.getKey())).andReturn(null); + replay(zc); + expect(parent.get(p)).andReturn("sekrit"); + replay(parent); + assertEquals("sekrit", c.get(Property.INSTANCE_SECRET)); + } + + @Test + public void testGetProperties() { + PropertyFilter filter = new AllFilter(); + Map props = new java.util.HashMap(); + parent.getProperties(props, filter); + replay(parent); + List children = new java.util.ArrayList(); + children.add("foo"); + children.add("ding"); + expect(zc.getChildren(ZooUtil.getRoot(iid) + Constants.ZTABLES + "/" + TID + Constants.ZTABLE_CONF)).andReturn(children); - expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZTABLES + "/" + TID + Constants.ZTABLE_CONF + "/" + "foo")).andReturn("bar".getBytes(Constants.UTF8)); - expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZTABLES + "/" + TID + Constants.ZTABLE_CONF + "/" + "ding")).andReturn("dong".getBytes(Constants.UTF8)); ++ expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZTABLES + "/" + TID + Constants.ZTABLE_CONF + "/" + "foo")).andReturn("bar".getBytes(UTF_8)); ++ expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZTABLES + "/" + TID + Constants.ZTABLE_CONF + "/" + "ding")).andReturn("dong".getBytes(UTF_8)); + replay(zc); + c.getProperties(props, filter); + assertEquals(2, props.size()); + assertEquals("bar", props.get("foo")); + assertEquals("dong", props.get("ding")); + } + + @Test + public void testObserver() { + ConfigurationObserver o = createMock(ConfigurationObserver.class); + c.addObserver(o); + Collection os = c.getObservers(); + assertEquals(1, os.size()); + assertTrue(os.contains(o)); + c.removeObserver(o); + os = c.getObservers(); + assertEquals(0, os.size()); + } + + @Test + public void testInvalidateCache() { + // need to do a get so the accessor is created + Property p = Property.INSTANCE_SECRET; + expect(zc.get(ZooUtil.getRoot(iid) + Constants.ZTABLES + "/" + TID + Constants.ZTABLE_CONF + "/" + p.getKey())) - .andReturn("sekrit".getBytes(Constants.UTF8)); ++ .andReturn("sekrit".getBytes(UTF_8)); + zc.clear(); + replay(zc); + c.get(Property.INSTANCE_SECRET); + c.invalidateCache(); + verify(zc); + } +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/9b20a9d4/server/base/src/test/java/org/apache/accumulo/server/conf/ZooCachePropertyAccessorTest.java ---------------------------------------------------------------------- diff --cc server/base/src/test/java/org/apache/accumulo/server/conf/ZooCachePropertyAccessorTest.java index 1b9d8f7,0000000..4369119 mode 100644,000000..100644 --- a/server/base/src/test/java/org/apache/accumulo/server/conf/ZooCachePropertyAccessorTest.java +++ b/server/base/src/test/java/org/apache/accumulo/server/conf/ZooCachePropertyAccessorTest.java @@@ -1,186 -1,0 +1,186 @@@ +/* + * 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.accumulo.server.conf; + ++import static com.google.common.base.Charsets.UTF_8; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +import java.util.List; +import java.util.Map; + - import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.conf.AccumuloConfiguration; +import org.apache.accumulo.core.conf.AccumuloConfiguration.PropertyFilter; +import org.apache.accumulo.core.conf.Property; +import org.apache.accumulo.fate.zookeeper.ZooCache; +import org.junit.Before; +import org.junit.Test; + +public class ZooCachePropertyAccessorTest { + private static final String PATH = "/root/path/to/props"; + private static final Property PROP = Property.INSTANCE_SECRET; + private static final String KEY = PROP.getKey(); + private static final String FULL_PATH = PATH + "/" + KEY; + private static final String VALUE = "value"; - private static final byte[] VALUE_BYTES = VALUE.getBytes(Constants.UTF8); ++ private static final byte[] VALUE_BYTES = VALUE.getBytes(UTF_8); + + private ZooCache zc; + private ZooCachePropertyAccessor a; + + @Before + public void setUp() { + zc = createMock(ZooCache.class); + a = new ZooCachePropertyAccessor(zc); + } + + @Test + public void testGetter() { + assertSame(zc, a.getZooCache()); + } + + @Test + public void testGet_Valid() { + expect(zc.get(FULL_PATH)).andReturn(VALUE_BYTES); + replay(zc); + assertEquals(VALUE, a.get(PROP, PATH, null)); + } + + @Test + public void testGet_Parent() { + AccumuloConfiguration parent = createMock(AccumuloConfiguration.class); + expect(parent.get(PROP)).andReturn(VALUE); + replay(parent); + expect(zc.get(FULL_PATH)).andReturn(null); + replay(zc); + assertEquals(VALUE, a.get(PROP, PATH, parent)); + } + + @Test + public void testGet_Parent_Null() { + AccumuloConfiguration parent = createMock(AccumuloConfiguration.class); + expect(parent.get(PROP)).andReturn(null); + replay(parent); + expect(zc.get(FULL_PATH)).andReturn(null); + replay(zc); + assertNull(a.get(PROP, PATH, parent)); + } + + @Test + public void testGet_Null_NoParent() { + expect(zc.get(FULL_PATH)).andReturn(null); + replay(zc); + assertNull(a.get(PROP, PATH, null)); + } + + @Test + public void testGet_InvalidFormat() { + Property badProp = Property.MASTER_CLIENTPORT; + expect(zc.get(PATH + "/" + badProp.getKey())).andReturn(VALUE_BYTES); + replay(zc); + AccumuloConfiguration parent = createMock(AccumuloConfiguration.class); + expect(parent.get(badProp)).andReturn("12345"); + replay(parent); + assertEquals("12345", a.get(badProp, PATH, parent)); + } + + @Test + public void testGetProperties() { + Map props = new java.util.HashMap(); + AccumuloConfiguration parent = createMock(AccumuloConfiguration.class); + PropertyFilter filter = createMock(PropertyFilter.class); + parent.getProperties(props, filter); + replay(parent); + String child1 = "child1"; + String child2 = "child2"; + List children = new java.util.ArrayList(); + children.add(child1); + children.add(child2); + expect(zc.getChildren(PATH)).andReturn(children); + expect(zc.get(PATH + "/" + child1)).andReturn(VALUE_BYTES); + expect(zc.get(PATH + "/" + child2)).andReturn(null); + replay(zc); + expect(filter.accept(child1)).andReturn(true); + expect(filter.accept(child2)).andReturn(true); + replay(filter); + + a.getProperties(props, PATH, filter, parent, null); + assertEquals(1, props.size()); + assertEquals(VALUE, props.get(child1)); + verify(parent); + } + + @Test + public void testGetProperties_NoChildren() { + Map props = new java.util.HashMap(); + AccumuloConfiguration parent = createMock(AccumuloConfiguration.class); + PropertyFilter filter = createMock(PropertyFilter.class); + parent.getProperties(props, filter); + replay(parent); + expect(zc.getChildren(PATH)).andReturn(null); + replay(zc); + + a.getProperties(props, PATH, filter, parent, null); + assertEquals(0, props.size()); + } + + @Test + public void testGetProperties_Filter() { + Map props = new java.util.HashMap(); + AccumuloConfiguration parent = createMock(AccumuloConfiguration.class); + PropertyFilter filter = createMock(PropertyFilter.class); + parent.getProperties(props, filter); + replay(parent); + String child1 = "child1"; + List children = new java.util.ArrayList(); + children.add(child1); + expect(zc.getChildren(PATH)).andReturn(children); + replay(zc); + expect(filter.accept(child1)).andReturn(false); + replay(filter); + + a.getProperties(props, PATH, filter, parent, null); + assertEquals(0, props.size()); + } + + @Test + public void testGetProperties_ParentFilter() { + Map props = new java.util.HashMap(); + AccumuloConfiguration parent = createMock(AccumuloConfiguration.class); + PropertyFilter filter = createMock(PropertyFilter.class); + PropertyFilter parentFilter = createMock(PropertyFilter.class); + parent.getProperties(props, parentFilter); + replay(parent); + expect(zc.getChildren(PATH)).andReturn(null); + replay(zc); + + a.getProperties(props, PATH, filter, parent, parentFilter); + verify(parent); + } + + @Test + public void testInvalidateCache() { + zc.clear(); + replay(zc); + a.invalidateCache(); + verify(zc); + } +}