Author: ieugen
Date: Sat Mar 31 09:10:31 2012
New Revision: 1307744
URL: http://svn.apache.org/viewvc?rev=1307744&view=rev
Log:
MAILBOX-103
- implementd UID generation with curator framework
- one test fails, probably a bug.
Modified:
james/mailbox/trunk/zoo-seq-provider/pom.xml
james/mailbox/trunk/zoo-seq-provider/src/main/java/org/apache/james/mailbox/store/mail/ZooUidProvider.java
james/mailbox/trunk/zoo-seq-provider/src/test/java/org/apache/james/mailbox/store/mail/ZooUidProviderTest.java
Modified: james/mailbox/trunk/zoo-seq-provider/pom.xml
URL:
http://svn.apache.org/viewvc/james/mailbox/trunk/zoo-seq-provider/pom.xml?rev=1307744&r1=1307743&r2=1307744&view=diff
==============================================================================
--- james/mailbox/trunk/zoo-seq-provider/pom.xml (original)
+++ james/mailbox/trunk/zoo-seq-provider/pom.xml Sat Mar 31 09:10:31 2012
@@ -48,8 +48,8 @@
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
- <artifactId>slf4j-simple</artifactId>
- <scope>test</scope>
+ <artifactId>slf4j-jdk14</artifactId>
+ <version>1.6.4</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
@@ -69,5 +69,16 @@
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
+ <dependency>
+ <groupId>com.netflix.curator</groupId>
+ <artifactId>curator-client</artifactId>
+ <version>${curator.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.netflix.curator</groupId>
+ <artifactId>curator-test</artifactId>
+ <version>${curator.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
Modified:
james/mailbox/trunk/zoo-seq-provider/src/main/java/org/apache/james/mailbox/store/mail/ZooUidProvider.java
URL:
http://svn.apache.org/viewvc/james/mailbox/trunk/zoo-seq-provider/src/main/java/org/apache/james/mailbox/store/mail/ZooUidProvider.java?rev=1307744&r1=1307743&r2=1307744&view=diff
==============================================================================
---
james/mailbox/trunk/zoo-seq-provider/src/main/java/org/apache/james/mailbox/store/mail/ZooUidProvider.java
(original)
+++
james/mailbox/trunk/zoo-seq-provider/src/main/java/org/apache/james/mailbox/store/mail/ZooUidProvider.java
Sat Mar 31 09:10:31 2012
@@ -18,7 +18,12 @@
****************************************************************/
package org.apache.james.mailbox.store.mail;
+import com.google.common.base.Preconditions;
+import com.netflix.curator.RetryPolicy;
import com.netflix.curator.framework.CuratorFramework;
+import com.netflix.curator.framework.recipes.atomic.AtomicValue;
+import com.netflix.curator.framework.recipes.atomic.DistributedAtomicLong;
+import com.netflix.curator.retry.RetryOneTime;
import java.io.Closeable;
import java.io.IOException;
import org.apache.james.mailbox.MailboxSession;
@@ -30,36 +35,70 @@ import org.apache.james.mailbox.store.ma
*/
public class ZooUidProvider<E> implements UidProvider<E>, Closeable {
- /** Inject the curator client using srping */
+ public final String UID_PATH_SUFIX = "-uid";
private final CuratorFramework client;
+ private final RetryPolicy retryPolicy;
public ZooUidProvider(CuratorFramework client) {
+ this(client, new RetryOneTime(1));
+ }
+
+ public ZooUidProvider(CuratorFramework client, RetryPolicy retryPolicy) {
+ Preconditions.checkNotNull(client);
+ Preconditions.checkNotNull(retryPolicy);
this.client = client;
- client.start();
+ this.retryPolicy = retryPolicy;
}
@Override
- public long nextUid(MailboxSession session,
- Mailbox<E> mailbox) throws MailboxException {
+ public long nextUid(MailboxSession session, Mailbox mailbox) throws
MailboxException {
if (client.isStarted()) {
- throw new UnsupportedOperationException("Not supported yet.");
- } else {
- throw new IllegalStateException("Curator client is closed.");
+ String path = mailbox.getMailboxId().toString() + UID_PATH_SUFIX;
+ System.out.println(path);
+ DistributedAtomicLong uid = new DistributedAtomicLong(client,
path, retryPolicy);
+ AtomicValue<Long> value = null;
+ try {
+ uid.increment();
+ value = uid.get();
+ } catch (Exception e) {
+ throw new MailboxException("Exception incrementing UID for
session " + session, e);
+ } finally {
+ if (value != null && value.succeeded()) {
+ return value.postValue();
+ } else {
+ throw new MailboxException("Failed getting next UID for "
+ session);
+ }
+ }
}
+ throw new MailboxException("Curator client is closed.");
}
@Override
- public long lastUid(MailboxSession session,
- Mailbox<E> mailbox) throws MailboxException {
+ public long lastUid(MailboxSession session, Mailbox<E> mailbox) throws
MailboxException {
if (client.isStarted()) {
- throw new UnsupportedOperationException("Not supported yet.");
- } else {
- throw new IllegalStateException("Curator client is closed.");
+ String path = mailbox.getMailboxId().toString() + UID_PATH_SUFIX;
+ System.out.println(path);
+ DistributedAtomicLong uid = new DistributedAtomicLong(client,
path, retryPolicy);
+ AtomicValue<Long> value = null;
+ try {
+ value = uid.get();
+ } catch (Exception e) {
+ throw new MailboxException("Exception getting last UID for
session " + session, e);
+ } finally {
+ if (value != null && value.succeeded()) {
+ return value.postValue();
+ } else {
+ throw new MailboxException("Failed getting last UID for "
+ session);
+ }
+ }
}
+ throw new MailboxException("Curator client is closed.");
}
@Override
public void close() throws IOException {
- client.close();
+ if (client.isStarted()) {
+ client.close();
+ }
}
}
Modified:
james/mailbox/trunk/zoo-seq-provider/src/test/java/org/apache/james/mailbox/store/mail/ZooUidProviderTest.java
URL:
http://svn.apache.org/viewvc/james/mailbox/trunk/zoo-seq-provider/src/test/java/org/apache/james/mailbox/store/mail/ZooUidProviderTest.java?rev=1307744&r1=1307743&r2=1307744&view=diff
==============================================================================
---
james/mailbox/trunk/zoo-seq-provider/src/test/java/org/apache/james/mailbox/store/mail/ZooUidProviderTest.java
(original)
+++
james/mailbox/trunk/zoo-seq-provider/src/test/java/org/apache/james/mailbox/store/mail/ZooUidProviderTest.java
Sat Mar 31 09:10:31 2012
@@ -18,11 +18,18 @@
****************************************************************/
package org.apache.james.mailbox.store.mail;
+import com.netflix.curator.RetryPolicy;
+import com.netflix.curator.framework.CuratorFramework;
+import com.netflix.curator.framework.CuratorFrameworkFactory;
+import com.netflix.curator.retry.RetryOneTime;
+import com.netflix.curator.test.TestingServer;
import java.util.UUID;
-import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.store.mail.model.Mailbox;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
+import static org.junit.Assert.*;
+import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
/**
@@ -30,20 +37,46 @@ import org.junit.Test;
*/
public class ZooUidProviderTest {
+ private static TestingServer testServer;
+ private static final int ZOO_TEST_PORT = 3123;
+ private final RetryPolicy retryPolicy = new RetryOneTime(1);
+ private CuratorFramework client;
+ private ZooUidProvider<UUID> uuidProvider;
+ private ZooUidProvider<Long> longProvider;
+ private SimpleMailbox<UUID> mailboxUUID;
+ private SimpleMailbox<Long> mailboxLong;
+ private UUID randomUUID = UUID.randomUUID();
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ testServer = new TestingServer(ZOO_TEST_PORT);
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ client = CuratorFrameworkFactory.builder().connectString("localhost:"
+ ZOO_TEST_PORT).retryPolicy(retryPolicy).
+ namespace("JAMES").build();
+ client.start();
+ uuidProvider = new ZooUidProvider<UUID>(client, retryPolicy);
+ longProvider = new ZooUidProvider<Long>(client, retryPolicy);
+ MailboxPath path1 = new MailboxPath("namespacetest", "namespaceuser",
"UUID");
+ MailboxPath path2 = new MailboxPath("namespacetest", "namespaceuser",
"Long");
+ mailboxUUID = new SimpleMailbox<UUID>(path1, 1L);
+ mailboxUUID.setMailboxId(randomUUID);
+ mailboxLong = new SimpleMailbox<Long>(path2, 2L);
+ mailboxLong.setMailboxId(123L);
+ }
+
/**
* Test of nextUid method, of class ZooUidProvider.
*/
@Test
public void testNextUid() throws Exception {
- System.out.println("nextUid");
- MailboxSession session = null;
- Mailbox<UUID> mailbox = null;
- ZooUidProvider instance = null;
- long expResult = 0L;
- long result = instance.nextUid(session, mailbox);
- assertEquals(expResult, result);
- // TODO review the generated test code and remove the default call to
fail.
- fail("The test case is a prototype.");
+ System.out.println("Testing nextUid");
+ long result = uuidProvider.nextUid(null, mailboxUUID);
+ assertEquals("Next UID is 1", 1, result);
+ result = longProvider.nextUid(null, mailboxLong);
+ assertEquals("Next UID is 1", 1, result);
}
/**
@@ -51,15 +84,15 @@ public class ZooUidProviderTest {
*/
@Test
public void testLastUid() throws Exception {
- System.out.println("lastUid");
- MailboxSession session = null;
- Mailbox<UUID> mailbox = null;
- ZooUidProvider instance = null;
- long expResult = 0L;
- long result = instance.lastUid(session, mailbox);
- assertEquals(expResult, result);
- // TODO review the generated test code and remove the default call to
fail.
- fail("The test case is a prototype.");
+ System.out.println("Testing lastUid");
+ long result = uuidProvider.lastUid(null, mailboxUUID);
+ assertEquals("Next UID is 0", 0, result);
+ result = uuidProvider.nextUid(null, mailboxUUID);
+ assertEquals("Next UID is 1", 1, result);
+ result = longProvider.lastUid(null, mailboxLong);
+ assertEquals("Next UID is 0", 0, result);
+ result = longProvider.nextUid(null, mailboxLong);
+ assertEquals("Next UID is 1", 1, result);
}
/**
@@ -67,10 +100,28 @@ public class ZooUidProviderTest {
*/
@Test
public void testClose() throws Exception {
- System.out.println("close");
- ZooUidProvider instance = null;
- instance.close();
- // TODO review the generated test code and remove the default call to
fail.
- fail("The test case is a prototype.");
+ System.out.println("Testing close");
+ uuidProvider.close();
+ try {
+ uuidProvider.lastUid(null, mailboxUUID);
+ } catch (MailboxException me) {
+ assertEquals("Curator client is closed.", me.getMessage());
+ }
+ try {
+ uuidProvider.nextUid(null, mailboxUUID);
+ } catch (MailboxException me) {
+ assertEquals("Curator client is closed.", me.getMessage());
+ }
+ longProvider.close();
+ try {
+ longProvider.lastUid(null, mailboxLong);
+ } catch (MailboxException me) {
+ assertEquals("Curator client is closed.", me.getMessage());
+ }
+ try {
+ longProvider.nextUid(null, mailboxLong);
+ } catch (MailboxException me) {
+ assertEquals("Curator client is closed.", me.getMessage());
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]