Skip to content

Instantly share code, notes, and snippets.

@n-dobryukha
Last active April 27, 2016 14:02
Show Gist options
  • Select an option

  • Save n-dobryukha/b4e5ab88f29dc36b91a2 to your computer and use it in GitHub Desktop.

Select an option

Save n-dobryukha/b4e5ab88f29dc36b91a2 to your computer and use it in GitHub Desktop.
package com.ndobryukha.tests.infinispan.entity;
/**
* Created by Nikita_Dobriukha on 2016-03-17.
*/
public class Book {
String title;
String description;
int publishYear;
public Book() {
}
public Book(String title, String description, int publishYear) {
this.title = title;
this.description = description;
this.publishYear = publishYear;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getPublishYear() {
return publishYear;
}
public void setPublishYear(int publishYear) {
this.publishYear = publishYear;
}
}
package com.ndobryukha.tests.infinispan.entity;
message Book {
required string title = 1;
required string description = 2;
required int32 publishYear = 3;
}
package com.ndobryukha.tests.infinispan.marshaller;
import com.ndobryukha.tests.infinispan.entity.Book;
import org.infinispan.protostream.MessageMarshaller;
import java.io.IOException;
/**
* Created by Nikita_Dobriukha on 2016-03-17.
*/
public class BookMarshaller implements MessageMarshaller<Book> {
@Override
public Book readFrom(ProtoStreamReader reader) throws IOException {
String title = reader.readString("title");
String description = reader.readString("description");
int publishYear = reader.readInt("publishYear");
return new Book(title, description, publishYear);
}
@Override
public void writeTo(ProtoStreamWriter writer, Book book) throws IOException {
writer.writeString("title", book.getTitle());
writer.writeString("description", book.getDescription());
writer.writeInt("publishYear", book.getPublishYear());
}
@Override
public Class<? extends Book> getJavaClass() {
return Book.class;
}
@Override
public String getTypeName() {
return "com.ndobryukha.tests.infinispan.entity.Book";
}
}
...
<subsystem xmlns="urn:infinispan:server:core:8.2" default-cache-container="clustered">
<cache-container name="clustered" default-cache="default" statistics="true">
<transport lock-timeout="60000"/>
<replicated-cache-configuration name="replicated" mode="SYNC" remote-timeout="25000" start="EAGER">
<indexing index="NONE"/>
<locking concurrency-level="500"/>
<state-transfer timeout="480000"/>
</replicated-cache-configuration>
<replicated-cache-configuration name="index-service-cache" mode="SYNC" start="EAGER" remote-timeout="25000">
<locking concurrency-level="500"/>
<state-transfer timeout="480000"/>
</replicated-cache-configuration>
<replicated-cache-configuration name="indexed-cache" mode="SYNC" start="EAGER" remote-timeout="20000"/>
<!-- book-cache -->
<replicated-cache name="indexMetadataBooksCache" configuration="index-service-cache"/>
<replicated-cache name="indexDataBooksCache" configuration="index-service-cache"/>
<replicated-cache name="indexLockingBooksCache" configuration="index-service-cache"/>
<replicated-cache name="booksCache" configuration="indexed-cache">
<indexing index="LOCAL">
<property name="default.metadata_cachename">indexMetadataBooksCache</property>
<property name="default.data_cachename">indexDataBooksCache</property>
<property name="default.locking_cachename">indexLockingBooksCache</property>
<property name="default.directory_provider">infinispan</property>
<property name="default.indexmanager">org.infinispan.query.indexmanager.InfinispanIndexManager</property>
<property name="lucene_version">LUCENE_CURRENT</property>
</indexing>
</replicated-cache>
<distributed-cache name="usersCache" mode="SYNC" start="EAGER">
<indexing index="LOCAL">
<property name="default.indexmanager">org.infinispan.query.indexmanager.InfinispanIndexManager</property>
</indexing>
</distributed-cache>
</subsystem>
...
package com.ndobryukha.tests.infinispan;
import com.ndobryukha.tests.infinispan.entity.Book;
import com.ndobryukha.tests.infinispan.entity.User;
import com.ndobryukha.tests.infinispan.marshaller.BookMarshaller;
import com.ndobryukha.tests.infinispan.marshaller.UserMarshaller;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.Search;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.logging.Log;
import org.infinispan.client.hotrod.logging.LogFactory;
import org.infinispan.client.hotrod.marshall.ProtoStreamMarshaller;
import org.infinispan.commons.util.Util;
import org.infinispan.protostream.FileDescriptorSource;
import org.infinispan.protostream.SerializationContext;
import org.infinispan.query.dsl.Query;
import org.infinispan.query.dsl.QueryFactory;
import org.infinispan.query.remote.client.MarshallerRegistration;
import org.infinispan.query.remote.client.ProtobufMetadataManagerConstants;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
/**
* Created by Nikita_Dobriukha on 2016-03-18.
*/
public class TestRealHotRodServer extends Assert {
private static final Log LOG = LogFactory.getLog(TestRealHotRodServer.class);
private static final String DEFAULT_CACHE_NAME = "default";
private static final String BOOKS_CACHE_NAME = "booksCache";
private static final String USERS_CACHE_NAME = "usersCache";
private static final RemoteCacheManager remoteCacheManager = createRemoteCacheManager();
@BeforeClass
public static void setUp() throws Exception {
configureRemoteCacheManager();
}
@AfterClass
public static void tearDown() throws Exception {
remoteCacheManager.stop();
}
private static RemoteCacheManager createRemoteCacheManager() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer()
.host("10.11.13.220")
.port(11222)
.marshaller(new ProtoStreamMarshaller());
return new RemoteCacheManager(builder.build());
}
private static void configureRemoteCacheManager() throws IOException {
RemoteCache<String, String> metadataCache = remoteCacheManager.getCache(ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME);
metadataCache.put("book.proto", Util.read(TestRealHotRodServer.class.getResourceAsStream("/book.proto")));
metadataCache.put("user.proto", Util.read(TestRealHotRodServer.class.getResourceAsStream("/user.proto")));
assertFalse(metadataCache.containsKey(ProtobufMetadataManagerConstants.ERRORS_KEY_SUFFIX));
SerializationContext srzCtx = ProtoStreamMarshaller.getSerializationContext(remoteCacheManager);
FileDescriptorSource fds = new FileDescriptorSource();
fds.addProtoFiles("/book.proto");
fds.addProtoFiles("/user.proto");
srzCtx.registerProtoFiles(fds);
srzCtx.registerMarshaller(new BookMarshaller());
srzCtx.registerMarshaller(new UserMarshaller());
MarshallerRegistration.registerMarshallers(srzCtx);
}
@Test
public void testDefaultCache() {
int count = 50;
RemoteCache<String, String> cache = remoteCacheManager.getCache(DEFAULT_CACHE_NAME);
for (int i=0; i<count; i++) {
cache.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}
assertEquals(count, cache.size());
cache.clear();
}
@Test
public void testBooksCache() throws IOException {
RemoteCache<Long, Book> cache = remoteCacheManager.getCache(BOOKS_CACHE_NAME);
cache.put(UUID.randomUUID().getMostSignificantBits(), new Book("title", "description", 2000));
QueryFactory qf = Search.getQueryFactory(cache);
Query query = qf.from(Book.class)
.having("title").eq("title").toBuilder()
.build();
List<Book> list = query.list();
assertNotNull(list);
cache.clear();
assertFalse(list.isEmpty());
}
@Test
public void testBooksCacheBenchmark() {
final int MAX_CACHE_COUNT = 100_000;
RemoteCache<Long, Book> cache = remoteCacheManager.getCache(BOOKS_CACHE_NAME);
for (int i = 0; i < MAX_CACHE_COUNT; i++) {
cache.put(UUID.randomUUID().getMostSignificantBits(), new Book("title" + i, "description" + i, 2000));
}
QueryFactory qf = Search.getQueryFactory(cache);
for (int i = 0; i < 250; i++) {
Query query = qf.from(Book.class).having("title").eq("title" + i*100).toBuilder().build();
long startSearch = System.currentTimeMillis();
List<Book> list = query.list();
long finish = System.currentTimeMillis();
LOG.info(String.format("search #%d: time = %.3fs", i, (finish - startSearch)/1000.0));
}
cache.clear();
}
@Test
public void testUsersCache() throws IOException {
RemoteCache<Long, User> cache = remoteCacheManager.getCache(USERS_CACHE_NAME);
cache.put(UUID.randomUUID().getMostSignificantBits(), new User(UUID.randomUUID().getMostSignificantBits(), "userName"));
QueryFactory qf = Search.getQueryFactory(cache);
Query query = qf.from(User.class)
.having("userName").eq("userName").toBuilder()
.build();
List<User> list = query.list();
assertNotNull(list);
cache.clear();
assertFalse(list.isEmpty());
}
}
package com.ndobryukha.tests.infinispan.entity;
/**
* Created by Nikita_Dobriukha on 2016-03-21.
*/
public class User {
private long id;
private String userName;
public User() {
}
public User(long id, String userName) {
this.id = id;
this.userName = userName;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
package com.ndobryukha.tests.infinispan.entity;
message User {
required int64 id = 1;
required string userName = 2;
}
package com.ndobryukha.tests.infinispan.marshaller;
import com.ndobryukha.tests.infinispan.entity.User;
import org.infinispan.protostream.MessageMarshaller;
import java.io.IOException;
/**
* Created by Nikita_Dobriukha on 2016-03-21.
*/
public class UserMarshaller implements MessageMarshaller<User> {
@Override
public User readFrom(ProtoStreamReader reader) throws IOException {
Long id = reader.readLong("id");
String userName = reader.readString("userName");
return new User(id, userName);
}
@Override
public void writeTo(ProtoStreamWriter writer, User user) throws IOException {
writer.writeLong("id", user.getId());
writer.writeString("userName", user.getUserName());
}
@Override
public Class<? extends User> getJavaClass() {
return User.class;
}
@Override
public String getTypeName() {
return "com.ndobryukha.tests.infinispan.entity.User";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment