Easy Scaling with Open Source Data Structures
TALIP OZTURK FOUNDER & CTO © 2014 Hazelcast Inc. Keywords
• In-Memory Data Grid! • Distributed Cache! • NoSQL! • Clustering, Scalability, Partitioning! • Cloud Computing Map
import java.util.Map; import java.util.HashMap;! ! Map map = new HashMap();! map.put(“1”, “value”); map.get(“1”); Concurrent Map
import java.util.Map; import java.util.concurrent.*;! ! Map map = new ConcurrentHashMap();! map.put(“1”, “value”); map.get(“1”); Distributed Map
import java.util.Map; import com.hazelcast.core.*;! ! HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(new Config()) Distributed Map
import java.util.Map; import com.hazelcast.core.*;! ! Map map = hazelcast.getMap(“mymap”);! map.put(“1”, “value”); map.get(“1”); Why Hazelcast?
• Session/state sharing! • Distributed Caching! • Messaging! • Distributed Processing Introducing Hazelcast
• Open source (Apache v2) • 5MB single jar • no-dependency • maven-friendly
© 2015 Hazelcast Inc. Confidential and Proprietary Data Partitioning in a Cluster
• Fixed number of partitions (default 271)! • Each key falls into a partition! • partitionId = hash(keyData)%PARTITION_COUNT! • Partition ownerships are reassigned upon membership change! Embedded
© 2015 Hazelcast Inc. Confidential and Proprietary Client-Server
© 2015 Hazelcast Inc. Confidential and Proprietary Client-Server
© 2015 Hazelcast Inc. Confidential and Proprietary Code Samples Hazelcast
• Hazelcast is thread safe Map
import com.hazelcast.core.*; ! import java.util.Set;! ! Cluster cluster = hazelcast.getCluster(); cluster.addMembershipListener(listener);! ! Member localMember = cluster.getLocalMember(); ! System.out.println (localMember.getInetAddress());! ! Set
import com.hazelcast.core.*; ! import java.util.ConcurrentMap;! ! Map
import com.hazelcast.core.*;! ! MultiMap
import com.hazelcast.core.*; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; ! BlockingQueue
import com.hazelcast.core.*; import java.util.concurrent.locks.Lock; ! Lock mylock = hazelcast.getLock(mylockobject); mylock.lock(); try { // do something } finally { mylock.unlock(); } ! //Lock on Map! IMap
import com.hazelcast.core.*; ! public class Sample implements MessageListener { public static void main(String[] args) { Sample sample = new Sample(); ITopic
import com.hazelcast.core.*; ! public class Sample implements EntryListener { public static void main(String[] args) { Sample sample = new Sample(); IMap map = hazelcast.getMap ("default"); map.addEntryListener (sample, true); map.addEntryListener (sample, "key"); } public void entryAdded(EntryEvent event) { System.out.println("Added " + event.getKey() + ":" + event.getValue()); } public void entryRemoved(EntryEvent event) { System.out.println("Removed " + event.getKey() + ":" + event.getValue()); } public void entryUpdated(EntryEvent event) { System.out.println("Updated " + event.getKey() + ":" + event.getValue()); } } Transactions
import com.hazelcast.core.*; import java.util.*; ! TransactionOptions options = new TransactionOptions().setTransactionType(TransactionType.TWO_PHASE); TransactionContext context = hz.newTransactionContext(options) context.beginTransaction(); ! TransactionalQueue queue = context.getQueue("myqueue"); TransactionalMap map = context.getMap ("mymap"); TransactionalSet set = context.getSet ("myset"); ! try { Object obj = queue.poll(); //process obj map.put ("1", "value1"); set.add ("value"); //do other things.. context.commitTransaction(); }catch (Throwable t) { context.rollbackTransaction(); } Data Afnity Data Affinity Data Affinity
Map
• A simple task public class HelloTask implements Callable
• On a specific node! • On any available node! • On a collection of defined nodes! • On a node owning a key ExecutorService executor = hazelcast.getExecutorService(); FutureTask
! public int removeOrder(long customerId, long orderId){! IMap
! public class OrderDeletionTask implements Callable
! public static int removeOrder(long customerId, long orderId){! ExecutorService es = Hazelcast.getExecutorService();! OrderDeletionTask task = new OrderDeletionTask(customerId, orderId);! Future future = es.submit(task);! int remainingOrders = future.get();! return remainingOrders;! }! EntryProcessor
© 2015 Hazelcast Inc. Confidential and Proprietary EntryProcessor
© 2015 Hazelcast Inc. Confidential and Proprietary Query Code%Samples%–%Query public class Customer { private boolean active; private String name; private int age;
// getters ! // setters } Code%Samples%–%Query import com.hazelcast.core.*; import com.hazelcast.query.SqlPredicate; import java.util.Collection;
IMap map = hazelcast.getMap(“customers”); ! ! map.addIndex(“active” ,false); map.addIndex(“name” ,false); map.addIndex(“age” ,true); ! ! Collection
© 2015 Hazelcast Inc. Confidential and Proprietary Code%Samples%–%Persistence import com.hazelcast.core.MapStore, import com.hazelcast.core.MapLoader, ! public class MyMapStore implements MapStore, MapLoader {
public Set loadAllKeys () { return readKeys(); } ! public Object load (Object key) { return readFromDatabase(key); } ! public void store (Object key, Object value) { saveIntoDatabase(key, value); } ! public void remove(Object key) { removeFromDatabase(key); } ! } Persistence
• Write7Behind%:%asynchronously%storing%entries% • Write7Through%:%synchronous% • Read7Through%:%if%get(key)%is%null,%load%it Configuration
• Distributed implementation of ! • Embedded, but accessible • Map! through! • Queue! • Native Java, C++, C# Clients! • Set! • REST! • List! • Memcache! • MultiMap! • Dynamic! • Lock! • Cluster! • Executor Service! • Add/ remove nodes! • Topic! • Backup! • Semaphore! • Fail-over • AtomicLong! • CountDownLatch! ! ! Thank you!
[email protected] twitter: @oztalip
© 2014 Hazelcast Inc.