You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jm...@apache.org on 2006/11/23 00:19:05 UTC
svn commit: r478374 [2/2] - in
/incubator/tuscany/java/sca/services/persistence: ./ store.journal/
store.journal/src/ store.journal/src/main/ store.journal/src/main/java/
store.journal/src/main/java/org/ store.journal/src/main/java/org/apache/
store.jo...
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordEntry.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordEntry.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordEntry.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordEntry.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,80 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.io.Serializable;
+
+/**
+ * A journal store cache entry for a record
+ *
+ * @version $Rev$ $Date$
+ */
+public class RecordEntry {
+ private Serializable object;
+ private short operation;
+ private long expiration;
+ private long[] journalKeys;
+
+
+ /**
+ * Creates a new cached record
+ *
+ * @param object the object to serialize
+ * @param operation an <code>INSERT</code> or <code>UPDATE</code> operation
+ * @param journalKey the journal key for the block where the record is written
+ */
+ public RecordEntry(Serializable object, short operation, long[] journalKey, long expiration) {
+ assert object != null;
+ this.object = object;
+ this.operation = operation;
+ this.journalKeys = journalKey;
+ this.expiration = expiration;
+ }
+
+ /**
+ * Returns the object
+ *
+ * @return the object
+ */
+ public Serializable getObject() {
+ return object;
+ }
+
+ /**
+ * Returns the type of operation
+ *
+ * @return the type of operation
+ */
+ public short getOperation() {
+ return operation;
+ }
+
+ public long getExpiration() {
+ return expiration;
+ }
+
+ public long[] getJournalKeys() {
+ return journalKeys;
+ }
+
+ public void setJournalKeys(long[] journalKeys) {
+ this.journalKeys = journalKeys;
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordEntry.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordEntry.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordKey.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordKey.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordKey.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordKey.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,66 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.util.UUID;
+
+/**
+ * Used by the store cache to retrieve record entries
+ *
+ * @version $Rev$ $Date$
+ */
+public class RecordKey {
+
+ private UUID id;
+ private String ownerName;
+
+ public RecordKey(UUID id, String ownerName) {
+ this.id = id;
+ this.ownerName = ownerName;
+ }
+
+ public UUID getId() {
+ return id;
+ }
+
+ public String getOwnerName() {
+ return ownerName;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ final RecordKey recordKey = (RecordKey) o;
+ if (id != null ? !id.equals(recordKey.id) : recordKey.id != null) {
+ return false;
+ }
+ return !(ownerName != null ? !ownerName.equals(recordKey.ownerName) : recordKey.ownerName != null);
+ }
+
+ public int hashCode() {
+ int result;
+ result = id != null ? id.hashCode() : 0;
+ result = 29 * result + (ownerName != null ? ownerName.hashCode() : 0);
+ return result;
+ }
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordKey.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/RecordKey.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/SerializationHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/SerializationHelper.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/SerializationHelper.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/SerializationHelper.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,218 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * Utility methods for processing journal records
+ *
+ * @version $Rev$ $Date$
+ */
+public final class SerializationHelper {
+
+ private SerializationHelper() {
+ }
+
+ /**
+ * Serializes and object
+ *
+ * @param serializable the object to serialize
+ * @throws IOException
+ */
+ public static byte[] serialize(Serializable serializable) throws IOException {
+ ByteArrayOutputStream bas = new ByteArrayOutputStream();
+ ObjectOutputStream out = new ObjectOutputStream(bas);
+ out.writeObject(serializable);
+ return bas.toByteArray();
+ }
+
+ /**
+ * Deserializes an object using the TCCL
+ *
+ * @param bytes the serialized object byte array
+ */
+ public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
+ return new TCCLObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
+ }
+
+ /**
+ * Breaks a byte array into a series of blocks of the given size
+ *
+ * @param bytes the byte array to partition
+ * @param size the partition size
+ */
+ public static List<byte[]> partition(byte[] bytes, int size) {
+ assert size > 0;
+ List<byte[]> list = new ArrayList<byte[]>();
+ int pos = 0;
+ while (pos < bytes.length) {
+ if (pos + size > bytes.length) {
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ for (int i = pos; i < bytes.length; i++) {
+ stream.write(bytes[i]);
+ }
+ byte[] partition = stream.toByteArray();
+ list.add(partition);
+ pos = pos + partition.length;
+ } else {
+ byte[] partition = new byte[size];
+ for (int i = 0; i < size; i++) {
+ partition[i] = bytes[pos + i];
+ }
+ list.add(partition);
+ pos = pos + size;
+ }
+ }
+ return list;
+ }
+
+ /**
+ * Creates a serialized header entry that may be written to a log
+ *
+ * @param operation the operation type, i.e. {@link Header#INSERT}, {@link Header#UPDATE}, or {@link
+ * JournalStore#DELETE}
+ * @param numRecords the number of blocks that the record will be written two excluding the header block
+ * @param ownerId the id of the owner of the record
+ * @param id the id of the record unique to the owner
+ * @param expiration the record expirtation time in milliseconds
+ * @return a byte array containing the serialized header
+ * @throws IOException
+ */
+ public static byte[] serializeHeader(short operation, int numRecords, String ownerId, UUID id, long expiration)
+ throws IOException {
+ ByteArrayOutputStream stream = null;
+ ObjectOutputStream ostream = null;
+ try {
+ stream = new ByteArrayOutputStream();
+ ostream = new ObjectOutputStream(stream);
+ ostream.writeShort(operation);
+ ostream.writeInt(numRecords);
+ ostream.writeObject(ownerId);
+ ostream.writeLong(id.getMostSignificantBits());
+ ostream.writeLong(id.getLeastSignificantBits());
+ ostream.writeLong(expiration);
+ ostream.flush();
+ return stream.toByteArray();
+ } finally {
+ if (ostream != null) {
+ try {
+ ostream.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
+ }
+
+ /**
+ * Serializes a unique record id consisting of the owner id and the record's UUID
+ *
+ * @param ownerId the id of the owner, typically an SCAObject canonical name
+ * @param id the UUID associated with the record
+ * @return the serialized record byte array
+ * @throws IOException
+ */
+ public static byte[] serializeRecordId(String ownerId, UUID id)
+ throws IOException {
+ ByteArrayOutputStream stream = null;
+ ObjectOutputStream ostream = null;
+ try {
+ stream = new ByteArrayOutputStream();
+ ostream = new ObjectOutputStream(stream);
+ ostream.writeObject(ownerId);
+ ostream.writeLong(id.getMostSignificantBits());
+ ostream.writeLong(id.getLeastSignificantBits());
+ ostream.flush();
+ return stream.toByteArray();
+ } finally {
+ if (ostream != null) {
+ try {
+ ostream.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
+ }
+
+ /**
+ * Deserializes the given header. The first element from {@link Header#getFields()} is assumed to contain the header
+ * byte array to deserialize from
+ *
+ * @param header the header to deserialize
+ * @return the deserialized header
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ public static Header deserializeHeader(Header header) throws IOException, ClassNotFoundException {
+ ByteArrayInputStream bas = null;
+ ObjectInputStream stream = null;
+ try {
+ bas = new ByteArrayInputStream(header.getFields()[0]);
+ stream = new TCCLObjectInputStream(bas);
+ header.setOperation(stream.readShort());
+ header.setNumBlocks(stream.readInt());
+ header.setOwnerId((String) stream.readObject());
+ header.setMostSignificant(stream.readLong());
+ header.setLeastSignificant(stream.readLong());
+ header.setExpiration(stream.readLong());
+ return header;
+ } finally {
+ if (bas != null) {
+ try {
+ bas.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
+
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/SerializationHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/SerializationHelper.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/TCCLObjectInputStream.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/TCCLObjectInputStream.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/TCCLObjectInputStream.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/TCCLObjectInputStream.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,42 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.io.ObjectInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.ObjectStreamClass;
+
+/**
+ * Deserializes an object based on the thread context classloader
+ *
+ * @version $Rev$ $Date$
+ */
+public class TCCLObjectInputStream extends ObjectInputStream {
+ private final ClassLoader classLoader;
+
+ public TCCLObjectInputStream(InputStream in) throws IOException, SecurityException {
+ super(in);
+ this.classLoader = Thread.currentThread().getContextClassLoader();
+ }
+
+ protected Class resolveClass(ObjectStreamClass streamClass) throws IOException, ClassNotFoundException {
+ return classLoader.loadClass(streamClass.getName());
+ }
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/TCCLObjectInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/main/java/org/apache/tuscany/persistence/store/journal/TCCLObjectInputStream.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreAppendTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreAppendTestCase.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreAppendTestCase.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreAppendTestCase.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,132 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.util.UUID;
+
+import org.apache.tuscany.spi.component.SCAObject;
+
+import junit.framework.TestCase;
+import org.apache.tuscany.service.persistence.store.Store;
+import org.apache.tuscany.service.persistence.store.StoreMonitor;
+import org.easymock.IAnswer;
+import org.easymock.classextension.EasyMock;
+import org.objectweb.howl.log.LogEventListener;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JournalStoreAppendTestCase extends TestCase {
+ @SuppressWarnings({"FieldCanBeLocal"})
+ private JournalStore store;
+ private SCAObject owner;
+
+ public void testOrderedShutdown() throws Exception {
+ StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class);
+ Journal journal = EasyMock.createMock(Journal.class);
+ journal.setLogEventListener(EasyMock.isA(LogEventListener.class));
+ journal.open();
+ journal.close();
+ EasyMock.replay(journal);
+ store = new JournalStore(monitor, journal) {
+ };
+ store.init();
+ store.destroy();
+ EasyMock.verify(journal);
+ }
+
+ public void testAppendRecord() throws Exception {
+ StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class);
+ Journal journal = EasyMock.createMock(Journal.class);
+ journal.setLogEventListener(EasyMock.isA(LogEventListener.class));
+ journal.open();
+ final UUID id = UUID.randomUUID();
+ EasyMock.expect(journal.writeHeader(EasyMock.isA(byte[].class), EasyMock.eq(false)))
+ .andStubAnswer(new IAnswer() {
+ public Object answer() throws Throwable {
+ Header header = new Header();
+ header.setFields(new byte[][]{(byte[]) EasyMock.getCurrentArguments()[0]});
+ // deserialize the message to test the format is correct
+ SerializationHelper.deserializeHeader(header);
+ assertTrue("Operation not INSERT", Header.INSERT == header.getOperation());
+ assertTrue("Expiration incorrect", Store.NEVER == header.getExpiration());
+ assertTrue("Least significant id incorrect",
+ id.getLeastSignificantBits() == header.getLeastSignificant());
+ assertTrue("Most significant id incorrect",
+ id.getMostSignificantBits() == header.getMostSignificant());
+ assertTrue("Records incorrect", 1 == header.getNumBlocks());
+ assertTrue("Owner id incorrect", "foo".equals(header.getOwnerId()));
+ return 1L;
+ }
+ });
+ EasyMock.expect(journal.writeBlock(EasyMock.isA(byte[].class), EasyMock.isA(byte[].class), EasyMock.eq(true)))
+ .andStubAnswer(new IAnswer() {
+ public Object answer() throws Throwable {
+ byte[] payload = (byte[]) EasyMock.getCurrentArguments()[0];
+ assertTrue("Block data incorrect", "test".equals(SerializationHelper.deserialize(payload)));
+ return 1L;
+ }
+ });
+ journal.close();
+ EasyMock.replay(journal);
+ store = new JournalStore(monitor, journal) {
+ };
+ store.init();
+ store.appendRecord(owner, id, "test", Store.NEVER);
+ store.destroy();
+ EasyMock.verify(journal);
+ }
+
+ /**
+ * Verifies that a written record will be cached. This is verified by the fact that long-term storage is never
+ * accessed.
+ */
+ public void testAppendRecordCache() throws Exception {
+ StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class);
+ Journal journal = EasyMock.createMock(Journal.class);
+ journal.setLogEventListener(EasyMock.isA(LogEventListener.class));
+ journal.open();
+ final UUID id = UUID.randomUUID();
+ EasyMock.expect(journal.writeHeader(EasyMock.isA(byte[].class), EasyMock.eq(false))).andReturn(1L);
+ EasyMock.expect(journal.writeBlock(EasyMock.isA(byte[].class), EasyMock.isA(byte[].class), EasyMock.eq(true)))
+ .andReturn(1L);
+ journal.close();
+ EasyMock.replay(journal);
+ store = new JournalStore(monitor, journal) {
+ };
+ store.init();
+ store.appendRecord(owner, id, "test", Store.NEVER);
+ assertEquals("test", store.readRecord(owner, id));
+ store.destroy();
+ EasyMock.verify(journal);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ TestUtils.cleanupLog();
+ owner = EasyMock.createMock(SCAObject.class);
+ EasyMock.expect(owner.getCanonicalName()).andReturn("foo").atLeastOnce();
+ EasyMock.replay(owner);
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ TestUtils.cleanupLog();
+ }
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreAppendTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreAppendTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreOverflowTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreOverflowTestCase.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreOverflowTestCase.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreOverflowTestCase.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,90 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.util.UUID;
+
+import org.apache.tuscany.spi.component.SCAObject;
+
+import junit.framework.TestCase;
+import org.apache.tuscany.service.persistence.store.StoreMonitor;
+import org.easymock.classextension.EasyMock;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JournalStoreOverflowTestCase extends TestCase {
+ @SuppressWarnings({"FieldCanBeLocal"})
+ private JournalStore store;
+ private SCAObject owner;
+
+ /**
+ * Validates records are moved forward during a log overflow
+ *
+ * @throws Exception
+ */
+ public void testOverflow() throws Exception {
+ StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class);
+ store = new JournalStore(monitor);
+ store.setMaxBlocksPerFile(3);
+ store.init();
+ long expire = System.currentTimeMillis() + 200;
+ store.appendRecord(owner, UUID.randomUUID(), "test", expire);
+ store.appendRecord(owner, UUID.randomUUID(), "test", expire);
+ store.appendRecord(owner, UUID.randomUUID(), "test", expire);
+ store.appendRecord(owner, UUID.randomUUID(), "test", expire);
+ store.appendRecord(owner, UUID.randomUUID(), "test", expire); //
+ store.appendRecord(owner, UUID.randomUUID(), "test", expire);
+ Thread.sleep(250);
+ store.appendRecord(owner, UUID.randomUUID(), "test", expire + 20000);
+ store.appendRecord(owner, UUID.randomUUID(), "test", expire + 20000);
+ store.destroy();
+ }
+
+
+ public void testOverflowAtInsertHeader() throws Exception {
+
+ }
+
+ public void testOverflowAtUpdateHeader() throws Exception {
+
+ }
+
+ public void testOverflowAtDeleteHeader() throws Exception {
+
+ }
+
+ public void testOverflowAtBlock() throws Exception {
+
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ TestUtils.cleanupLog();
+ owner = EasyMock.createMock(SCAObject.class);
+ EasyMock.expect(owner.getCanonicalName()).andReturn("foo").atLeastOnce();
+ EasyMock.replay(owner);
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ TestUtils.cleanupLog();
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreOverflowTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreOverflowTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalTestCase.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalTestCase.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalTestCase.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,78 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.io.ByteArrayOutputStream;
+import java.util.UUID;
+
+import junit.framework.TestCase;
+import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serializeHeader;
+import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serializeRecordId;
+import static org.apache.tuscany.service.persistence.store.Store.NEVER;
+import org.objectweb.howl.log.LogRecord;
+import org.objectweb.howl.log.Configuration;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JournalTestCase extends TestCase {
+ private Journal journal;
+
+ public void testWriteHeader() throws Exception {
+ UUID id = UUID.randomUUID();
+ long key = journal.writeHeader(serializeHeader(Header.INSERT, 10, "foo/bar", id, NEVER), false);
+ LogRecord record = journal.get(null, key);
+ Header header = new Header();
+ header.setFields(record.getFields());
+ SerializationHelper.deserializeHeader(header);
+ assertTrue(record.type == Journal.HEADER);
+ assertEquals(Header.INSERT, header.getOperation());
+ assertEquals(10, header.getNumBlocks());
+ assertEquals("foo/bar", header.getOwnerId());
+ assertEquals(id.getMostSignificantBits(), header.getMostSignificant());
+ assertEquals(id.getLeastSignificantBits(), header.getLeastSignificant());
+ assertEquals(NEVER, header.getExpiration());
+ }
+
+ public void testWriteRecord() throws Exception {
+ byte[] recordId = serializeRecordId("foo", UUID.randomUUID());
+ long key = journal.writeBlock("this is a test".getBytes(), recordId, true);
+ LogRecord record = journal.get(null, key);
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ assertEquals(record.type, Journal.RECORD);
+ stream.write(record.getFields()[1]);
+ JournalRecord jrecord = new JournalRecord(stream.toByteArray());
+ assertEquals("this is a test", new String(jrecord.getData()));
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ TestUtils.cleanupLog();
+ Configuration config = new Configuration();
+ config.setLogFileDir("../stores");
+ journal = new Journal(config);
+ journal.open();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ journal.close();
+ TestUtils.cleanupLog();
+ }
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/LRUCacheTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/LRUCacheTestCase.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/LRUCacheTestCase.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/LRUCacheTestCase.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,104 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.util.UUID;
+
+import junit.framework.TestCase;
+import static org.apache.tuscany.service.persistence.store.Store.NEVER;
+import org.easymock.EasyMock;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class LRUCacheTestCase extends TestCase {
+
+ public void testEviction() {
+ LRUCache cache = new LRUCache(2);
+ RecordKey key1 = new RecordKey(UUID.randomUUID(), "foo");
+ RecordKey key2 = new RecordKey(UUID.randomUUID(), "bar");
+ RecordKey key3 = new RecordKey(UUID.randomUUID(), "baz");
+ RecordEntry entry1 = new RecordEntry("", Header.INSERT, null, NEVER);
+ RecordEntry entry2 = new RecordEntry("", Header.INSERT, null, NEVER);
+ RecordEntry entry3 = new RecordEntry("", Header.INSERT, null, NEVER);
+ cache.put(key1, entry1);
+ cache.put(key2, entry2);
+ cache.get(key1);
+ cache.put(key3, entry3);
+ assertNotNull(cache.get(key3));
+ assertNotNull(cache.get(key1));
+ assertNull(cache.get(key2));
+ }
+
+ public void testPut() {
+ LRUCache cache = new LRUCache(3);
+ RecordKey key1 = new RecordKey(UUID.randomUUID(), "foo");
+ RecordKey key2 = new RecordKey(UUID.randomUUID(), "bar");
+ RecordKey key3 = new RecordKey(UUID.randomUUID(), "baz");
+ RecordEntry entry1 = new RecordEntry("", Header.INSERT, null, NEVER);
+ RecordEntry entry2 = new RecordEntry("", Header.INSERT, null, NEVER);
+ RecordEntry entry3 = new RecordEntry("", Header.INSERT, null, NEVER);
+ cache.put(key1, entry1);
+ cache.put(key2, entry2);
+ cache.get(key1);
+ cache.put(key3, entry3);
+ assertNotNull(cache.get(key3));
+ assertNotNull(cache.get(key1));
+ assertNotNull(cache.get(key3));
+ }
+
+ public void testNotifyListenerOnEviction() {
+ LRUCache cache = new LRUCache(2);
+ RecordKey key1 = new RecordKey(UUID.randomUUID(), "foo");
+ RecordKey key2 = new RecordKey(UUID.randomUUID(), "bar");
+ RecordKey key3 = new RecordKey(UUID.randomUUID(), "baz");
+ RecordEntry entry1 = new RecordEntry("", Header.INSERT, null, NEVER);
+ RecordEntry entry2 = new RecordEntry("", Header.INSERT, null, NEVER);
+ RecordEntry entry3 = new RecordEntry("", Header.INSERT, null, NEVER);
+ CacheEventListener listener = EasyMock.createMock(CacheEventListener.class);
+ listener.onEviction(EasyMock.eq(key2), EasyMock.eq(entry2));
+ EasyMock.replay(listener);
+ cache.addListener(listener);
+ cache.put(key1, entry1);
+ cache.put(key2, entry2);
+ cache.get(key1);
+ cache.put(key3, entry3);
+ EasyMock.verify(listener);
+ }
+
+ public void testRemoveListener() {
+ LRUCache cache = new LRUCache(2);
+ RecordKey key1 = new RecordKey(UUID.randomUUID(), "foo");
+ RecordKey key2 = new RecordKey(UUID.randomUUID(), "bar");
+ RecordKey key3 = new RecordKey(UUID.randomUUID(), "baz");
+ RecordEntry entry1 = new RecordEntry("", Header.INSERT, null, NEVER);
+ RecordEntry entry2 = new RecordEntry("", Header.INSERT, null, NEVER);
+ RecordEntry entry3 = new RecordEntry("", Header.INSERT, null, NEVER);
+ CacheEventListener listener = EasyMock.createMock(CacheEventListener.class);
+ EasyMock.replay(listener);
+ cache.addListener(listener);
+ cache.put(key1, entry1);
+ cache.put(key2, entry2);
+ cache.get(key1);
+ cache.removeListener(listener);
+ cache.put(key3, entry3);
+ EasyMock.verify(listener);
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/LRUCacheTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/LRUCacheTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/RecordKeyTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/RecordKeyTestCase.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/RecordKeyTestCase.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/RecordKeyTestCase.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,51 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.util.UUID;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class RecordKeyTestCase extends TestCase {
+
+ public void testEquals() throws Exception {
+ UUID id = UUID.randomUUID();
+ RecordKey key1 = new RecordKey(id, "foo");
+ RecordKey key2 = new RecordKey(id, "foo");
+ assertEquals(key1, key2);
+ }
+
+ public void testNotEqualsId() throws Exception {
+ UUID id = UUID.randomUUID();
+ RecordKey key1 = new RecordKey(id, "foo");
+ RecordKey key2 = new RecordKey(UUID.randomUUID(), "foo");
+ assertFalse(key1.equals(key2));
+ }
+
+ public void testNotEqualsOwner() throws Exception {
+ UUID id = UUID.randomUUID();
+ RecordKey key1 = new RecordKey(id, "foo");
+ RecordKey key2 = new RecordKey(id, "bar");
+ assertFalse(key1.equals(key2));
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/RecordKeyTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/RecordKeyTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/SerializationHelperTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/SerializationHelperTestCase.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/SerializationHelperTestCase.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/SerializationHelperTestCase.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,82 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.util.List;
+import java.util.UUID;
+
+import junit.framework.TestCase;
+import static org.apache.tuscany.service.persistence.store.Store.NEVER;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class SerializationHelperTestCase extends TestCase {
+
+ public void testTwoEvenChunks() throws Exception {
+ byte[] bytes = "this is a test".getBytes();
+ List<byte[]> chunks = SerializationHelper.partition(bytes, 7);
+ assertEquals(2, chunks.size());
+ assertEquals("this is", new String(chunks.get(0)));
+ assertEquals(" a test", new String(chunks.get(1)));
+ }
+
+ public void testUnevenChunks() throws Exception {
+ byte[] bytes = "this is a test123".getBytes();
+ List<byte[]> chunks = SerializationHelper.partition(bytes, 7);
+ assertEquals(3, chunks.size());
+ assertEquals("this is", new String(chunks.get(0)));
+ assertEquals(" a test", new String(chunks.get(1)));
+ assertEquals("123", new String(chunks.get(2)));
+ }
+
+ public void testChunkSizeGreater() throws Exception {
+ byte[] bytes = "this is a test".getBytes();
+ List<byte[]> chunks = SerializationHelper.partition(bytes, 512);
+ assertEquals(1, chunks.size());
+ byte[] chunk = chunks.get(0);
+ assertEquals(14, chunk.length);
+ assertEquals("this is a test", new String(chunk));
+ }
+
+ public void testSerializeDeserialize() throws Exception {
+ byte[] bytes = SerializationHelper.serialize("foo");
+ assertEquals("foo", SerializationHelper.deserialize(bytes));
+ }
+
+ public void testDeserializeHeader() throws Exception {
+ UUID id = UUID.randomUUID();
+ byte[] bytes = SerializationHelper.serializeHeader(Header.INSERT, 2, "foo", id, NEVER);
+ Header header = SerializationHelper.deserializeHeader(new MockHeader(bytes));
+ assertEquals(Header.INSERT, header.getOperation());
+ assertEquals(2, header.getNumBlocks());
+ assertEquals("foo", header.getOwnerId());
+ assertEquals(id.getMostSignificantBits(), header.getMostSignificant());
+ assertEquals(id.getLeastSignificantBits(), header.getLeastSignificant());
+ assertEquals(NEVER, header.getExpiration());
+ }
+
+ private class MockHeader extends Header {
+ public MockHeader(byte[] bytes) {
+ super();
+ fields = new byte[][]{bytes};
+ }
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/SerializationHelperTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/SerializationHelperTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/TestUtils.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/TestUtils.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/TestUtils.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/TestUtils.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,47 @@
+/*
+ * 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.tuscany.persistence.store.journal;
+
+import java.io.File;
+
+/**
+ * JournalThroughputTest case utilities
+ *
+ * @version $Rev$ $Date$
+ */
+public final class TestUtils {
+
+ private TestUtils() {
+
+ }
+
+ /**
+ * Removes log files from disk
+ */
+ public static void cleanupLog() {
+ File dir = new File("../stores");
+ if (!dir.exists()) {
+ return;
+ }
+ for (File file : dir.listFiles()) {
+ file.delete();
+ }
+ dir.delete();
+ }
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/TestUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/TestUtils.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/Foo.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/Foo.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/Foo.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/Foo.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,52 @@
+/*
+ * 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.tuscany.persistence.store.journal.performance;
+
+import java.io.Serializable;
+
+/**
+ * @version $Rev$ $Date$
+ */
+@SuppressWarnings({"SerializableHasSerializationMethods"})
+public class Foo implements Serializable {
+ private static final long serialVersionUID = -1003664515513709814L;
+ protected String baz;
+ protected int bar;
+
+ public Foo(String baz, int bar) {
+ this.baz = baz;
+ this.bar = bar;
+ }
+
+ public String getBaz() {
+ return baz;
+ }
+
+ public void setBaz(String baz) {
+ this.baz = baz;
+ }
+
+ public int getBar() {
+ return bar;
+ }
+
+ public void setBar(int bar) {
+ this.bar = bar;
+ }
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/Foo.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/Foo.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalStoreThroughputTest.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalStoreThroughputTest.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalStoreThroughputTest.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalStoreThroughputTest.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,117 @@
+/*
+ * 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.tuscany.persistence.store.journal.performance;
+
+import java.io.IOException;
+import java.util.UUID;
+import java.util.concurrent.BrokenBarrierException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.tuscany.spi.component.SCAObject;
+
+import org.apache.tuscany.persistence.store.journal.JournalShutdownException;
+import org.apache.tuscany.persistence.store.journal.JournalStore;
+import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serialize;
+import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serializeRecordId;
+import org.apache.tuscany.persistence.store.journal.TestUtils;
+import org.apache.tuscany.service.persistence.store.StoreWriteException;
+
+/**
+ * Runs a basic throughput tests on JournalStore operations
+ * <p/>
+ * TODO this should be integrated with a Maven itest-based performance harness
+ *
+ * @version $Rev$ $Date$
+ */
+public class JournalStoreThroughputTest {
+ private static final int SIZE = 1000;
+ private CyclicBarrier barrier;
+ private JournalStore store;
+ private long now;
+ private SCAObject owner = new MockSCAObject();
+ private UUID id = UUID.randomUUID();
+ private CountDownLatch latch = new CountDownLatch(1);
+ private long expire = System.currentTimeMillis() + 10000;
+ private Foo object = new Foo("this is a test", 1);
+
+ public static void main(String[] args) throws Exception {
+ JournalStoreThroughputTest test = new JournalStoreThroughputTest();
+ test.testAppend();
+ test.latch.await(5000, TimeUnit.MILLISECONDS);
+ }
+
+ public void testAppend() throws Exception {
+ TestUtils.cleanupLog();
+ store = new JournalStore(new MockMonitor());
+ store.init();
+ final Thread[] threads = new Thread[SIZE];
+ barrier = new CyclicBarrier(SIZE, new Runnable() {
+ public void run() {
+ try {
+ System.out.println("-----------------------------------------------------");
+ System.out.println("JournalStore.append()");
+ byte[] idBytes = serializeRecordId(owner.getCanonicalName(), id);
+ byte[] bytes = serialize(object);
+ System.out.println("Approx record size :" + (bytes.length + idBytes.length));
+ System.out.println("Total threads :" + barrier.getNumberWaiting());
+ System.out.println("Forced writes :" + barrier.getNumberWaiting());
+ System.out.println("Time:" + (System.currentTimeMillis() - now));
+ store.destroy();
+ latch.countDown();
+ } catch (JournalShutdownException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ for (int i = 0; i < SIZE; i++) {
+ threads[i] = new Thread(new AppendWorker(true));
+ }
+ now = System.currentTimeMillis();
+ for (int i = 0; i < SIZE; i++) {
+ threads[i].start();
+ }
+ }
+
+ private class AppendWorker implements Runnable {
+ boolean forced;
+
+ public AppendWorker(boolean forced) {
+ this.forced = forced;
+ }
+
+ public void run() {
+ try {
+ store.appendRecord(owner, id, object, expire);
+ barrier.await();
+ } catch (StoreWriteException e) {
+ e.printStackTrace();
+ } catch (BrokenBarrierException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalStoreThroughputTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalStoreThroughputTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalThroughputTest.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalThroughputTest.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalThroughputTest.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalThroughputTest.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,145 @@
+/*
+ * 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.tuscany.persistence.store.journal.performance;
+
+import java.util.UUID;
+import java.util.concurrent.BrokenBarrierException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.TimeUnit;
+import java.io.IOException;
+
+import org.apache.tuscany.persistence.store.journal.Journal;
+import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serializeRecordId;
+import org.apache.tuscany.persistence.store.journal.TestUtils;
+import org.apache.tuscany.service.persistence.store.StoreWriteException;
+
+/**
+ * Runs a basic throughput tests on Journal operations
+ * <p/>
+ * TODO this should be integrated with a Maven itest-based performance harness
+ *
+ * @version $Rev$ $Date$
+ */
+public class JournalThroughputTest {
+ private static final int SIZE = 1000;
+ private CyclicBarrier barrier;
+ private Journal journal;
+ private byte[] bytes;
+ private byte[] recordId;
+ private long now;
+ private CountDownLatch latch = new CountDownLatch(1);
+
+ public static void main(String[] args) throws Exception {
+ JournalThroughputTest test = new JournalThroughputTest();
+ test.testForcedWrites();
+ test.latch.await(5000, TimeUnit.MILLISECONDS);
+ test.testNonForcedWrites();
+ }
+
+ public void testForcedWrites() throws Exception {
+ TestUtils.cleanupLog();
+ journal = new Journal();
+ journal.open();
+ recordId = serializeRecordId("foo", UUID.randomUUID());
+ bytes = "this is a test".getBytes();
+ final Thread[] threads = new Thread[SIZE];
+ barrier = new CyclicBarrier(SIZE, new Runnable() {
+ public void run() {
+ System.out.println("-----------------------------------------------------");
+ System.out.println("Journal.writeBlock() using forced writes");
+ System.out.println("Approx record size :" + (recordId.length + bytes.length));
+ System.out.println("Total threads :" + barrier.getNumberWaiting());
+ System.out.println("Forced writes :" + barrier.getNumberWaiting());
+ System.out.println("Time:" + (System.currentTimeMillis() - now));
+ try {
+ journal.close();
+ latch.countDown();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ for (int i = 0; i < SIZE; i++) {
+ threads[i] = new Thread(new Worker(true));
+ }
+ now = System.currentTimeMillis();
+ for (int i = 0; i < SIZE; i++) {
+ threads[i].start();
+ }
+ }
+
+ public void testNonForcedWrites() throws Exception {
+ TestUtils.cleanupLog();
+ journal = new Journal();
+ journal.open();
+ recordId = serializeRecordId("foo", UUID.randomUUID());
+ bytes = "this is a test".getBytes();
+ final Thread[] threads = new Thread[SIZE];
+ barrier = new CyclicBarrier(SIZE, new Runnable() {
+ public void run() {
+ System.out.println("-----------------------------------------------------");
+ System.out.println("Journal.writeBlock() using non-forced writes");
+ System.out.println("Approx record size :" + (recordId.length + bytes.length));
+ System.out.println("Total threads :" + barrier.getNumberWaiting());
+ System.out.println("Forced writes :" + barrier.getNumberWaiting());
+ System.out.println("Time:" + (System.currentTimeMillis() - now));
+ System.out.println("-----------------------------------------------------");
+ try {
+ journal.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ for (int i = 0; i < SIZE; i++) {
+ threads[i] = new Thread(new Worker(false));
+ }
+ now = System.currentTimeMillis();
+ for (int i = 0; i < SIZE; i++) {
+ threads[i].start();
+ }
+ }
+
+ private class Worker implements Runnable {
+ boolean forced;
+
+ public Worker(boolean forced) {
+ this.forced = forced;
+ }
+
+ public void run() {
+ try {
+ journal.writeBlock(bytes, recordId, forced);
+ barrier.await();
+ } catch (StoreWriteException e) {
+ e.printStackTrace();
+ } catch (BrokenBarrierException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalThroughputTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalThroughputTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockMonitor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockMonitor.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockMonitor.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockMonitor.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,57 @@
+/*
+ * 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.tuscany.persistence.store.journal.performance;
+
+import org.apache.tuscany.service.persistence.store.StoreMonitor;
+import org.apache.tuscany.api.annotation.LogLevel;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class MockMonitor implements StoreMonitor {
+ @LogLevel("DEBUG")
+ public void start(String msg) {
+
+ }
+
+ @LogLevel("DEBUG")
+ public void stop(String msg) {
+
+ }
+
+ @LogLevel("DEBUG")
+ public void beginRecover() {
+
+ }
+
+ @LogLevel("DEBUG")
+ public void endRecover() {
+
+ }
+
+ @LogLevel("DEBUG")
+ public void recover(Object recordId) {
+
+ }
+
+ @LogLevel("ERROR")
+ public void error(Throwable e) {
+
+ }
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockMonitor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockMonitor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockSCAObject.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockSCAObject.java?view=auto&rev=478374
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockSCAObject.java (added)
+++ incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockSCAObject.java Wed Nov 22 15:19:04 2006
@@ -0,0 +1,95 @@
+/*
+ * 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.tuscany.persistence.store.journal.performance;
+
+import java.util.Map;
+
+import org.apache.tuscany.spi.component.SCAObject;
+import org.apache.tuscany.spi.component.CompositeComponent;
+import org.apache.tuscany.spi.component.TargetException;
+import org.apache.tuscany.spi.model.Scope;
+import org.apache.tuscany.spi.event.Event;
+import org.apache.tuscany.spi.event.RuntimeEventListener;
+import org.apache.tuscany.spi.event.EventFilter;
+import org.apache.tuscany.spi.CoreRuntimeException;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class MockSCAObject implements SCAObject {
+ public String getName() {
+ return null;
+ }
+
+ public String getCanonicalName() {
+ return "foo";
+ }
+
+ public CompositeComponent getParent() {
+ return null;
+ }
+
+ public Scope getScope() {
+ return null;
+ }
+
+ public Object getServiceInstance() throws TargetException {
+ return null;
+ }
+
+ public void prepare() {
+
+ }
+
+ public Map<Object, Object> getExtensions() {
+ return null;
+ }
+
+ public boolean isSystem() {
+ return false;
+ }
+
+ public void publish(Event object) {
+
+ }
+
+ public void addListener(RuntimeEventListener listener) {
+
+ }
+
+ public void addListener(EventFilter filter, RuntimeEventListener listener) {
+
+ }
+
+ public void removeListener(RuntimeEventListener listener) {
+
+ }
+
+ public int getLifecycleState() {
+ return 0;
+ }
+
+ public void start() throws CoreRuntimeException {
+
+ }
+
+ public void stop() throws CoreRuntimeException {
+
+ }
+}
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockSCAObject.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockSCAObject.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org