本文主要研究一下storm trident的statehtml
storm-2.0.0/storm-client/src/jvm/org/apache/storm/trident/state/StateType.javajava
public enum StateType { NON_TRANSACTIONAL, TRANSACTIONAL, OPAQUE }
storm-2.0.0/storm-client/src/jvm/org/apache/storm/trident/state/State.java數據庫
/** * There's 3 different kinds of state: * * 1. non-transactional: ignores commits, updates are permanent. no rollback. a cassandra incrementing state would be like this 2. * repeat-transactional: idempotent as long as all batches for a txid are identical 3. opaque-transactional: the most general kind of state. * updates are always done based on the previous version of the value if the current commit = latest stored commit Idempotent even if the * batch for a txid can change. * * repeat transactional is idempotent for transactional spouts opaque transactional is idempotent for opaque or transactional spouts * * Trident should log warnings when state is idempotent but updates will not be idempotent because of spout */ // retrieving is encapsulated in Retrieval interface public interface State { void beginCommit(Long txid); // can be null for things like partitionPersist occuring off a DRPC stream void commit(Long txid); }
storm-2.0.0/storm-client/src/jvm/org/apache/storm/trident/state/map/MapState.javaapache
public interface MapState<T> extends ReadOnlyMapState<T> { List<T> multiUpdate(List<List<Object>> keys, List<ValueUpdater> updaters); void multiPut(List<List<Object>> keys, List<T> vals); }
storm-2.0.0/storm-client/src/jvm/org/apache/storm/trident/state/map/NonTransactionalMap.java緩存
public class NonTransactionalMap<T> implements MapState<T> { IBackingMap<T> _backing; protected NonTransactionalMap(IBackingMap<T> backing) { _backing = backing; } public static <T> MapState<T> build(IBackingMap<T> backing) { return new NonTransactionalMap<T>(backing); } @Override public List<T> multiGet(List<List<Object>> keys) { return _backing.multiGet(keys); } @Override public List<T> multiUpdate(List<List<Object>> keys, List<ValueUpdater> updaters) { List<T> curr = _backing.multiGet(keys); List<T> ret = new ArrayList<T>(curr.size()); for (int i = 0; i < curr.size(); i++) { T currVal = curr.get(i); ValueUpdater<T> updater = updaters.get(i); ret.add(updater.update(currVal)); } _backing.multiPut(keys, ret); return ret; } @Override public void multiPut(List<List<Object>> keys, List<T> vals) { _backing.multiPut(keys, vals); } @Override public void beginCommit(Long txid) { } @Override public void commit(Long txid) { } }
storm-2.0.0/storm-client/src/jvm/org/apache/storm/trident/state/map/TransactionalMap.javajvm
public class TransactionalMap<T> implements MapState<T> { CachedBatchReadsMap<TransactionalValue> _backing; Long _currTx; protected TransactionalMap(IBackingMap<TransactionalValue> backing) { _backing = new CachedBatchReadsMap(backing); } public static <T> MapState<T> build(IBackingMap<TransactionalValue> backing) { return new TransactionalMap<T>(backing); } @Override public List<T> multiGet(List<List<Object>> keys) { List<CachedBatchReadsMap.RetVal<TransactionalValue>> vals = _backing.multiGet(keys); List<T> ret = new ArrayList<T>(vals.size()); for (CachedBatchReadsMap.RetVal<TransactionalValue> retval : vals) { TransactionalValue v = retval.val; if (v != null) { ret.add((T) v.getVal()); } else { ret.add(null); } } return ret; } @Override public List<T> multiUpdate(List<List<Object>> keys, List<ValueUpdater> updaters) { List<CachedBatchReadsMap.RetVal<TransactionalValue>> curr = _backing.multiGet(keys); List<TransactionalValue> newVals = new ArrayList<TransactionalValue>(curr.size()); List<List<Object>> newKeys = new ArrayList(); List<T> ret = new ArrayList<T>(); for (int i = 0; i < curr.size(); i++) { CachedBatchReadsMap.RetVal<TransactionalValue> retval = curr.get(i); TransactionalValue<T> val = retval.val; ValueUpdater<T> updater = updaters.get(i); TransactionalValue<T> newVal; boolean changed = false; if (val == null) { newVal = new TransactionalValue<T>(_currTx, updater.update(null)); changed = true; } else { if (_currTx != null && _currTx.equals(val.getTxid()) && !retval.cached) { newVal = val; } else { newVal = new TransactionalValue<T>(_currTx, updater.update(val.getVal())); changed = true; } } ret.add(newVal.getVal()); if (changed) { newVals.add(newVal); newKeys.add(keys.get(i)); } } if (!newKeys.isEmpty()) { _backing.multiPut(newKeys, newVals); } return ret; } @Override public void multiPut(List<List<Object>> keys, List<T> vals) { List<TransactionalValue> newVals = new ArrayList<TransactionalValue>(vals.size()); for (T val : vals) { newVals.add(new TransactionalValue<T>(_currTx, val)); } _backing.multiPut(keys, newVals); } @Override public void beginCommit(Long txid) { _currTx = txid; _backing.reset(); } @Override public void commit(Long txid) { _currTx = null; _backing.reset(); } }
即不是本次事務中multiPut進去的
),那麼不會更新該值(skip the update
),使用newVal = valstorm-2.0.0/storm-client/src/jvm/org/apache/storm/trident/state/map/OpaqueMap.javaide
public class OpaqueMap<T> implements MapState<T> { CachedBatchReadsMap<OpaqueValue> _backing; Long _currTx; protected OpaqueMap(IBackingMap<OpaqueValue> backing) { _backing = new CachedBatchReadsMap(backing); } public static <T> MapState<T> build(IBackingMap<OpaqueValue> backing) { return new OpaqueMap<T>(backing); } @Override public List<T> multiGet(List<List<Object>> keys) { List<CachedBatchReadsMap.RetVal<OpaqueValue>> curr = _backing.multiGet(keys); List<T> ret = new ArrayList<T>(curr.size()); for (CachedBatchReadsMap.RetVal<OpaqueValue> retval : curr) { OpaqueValue val = retval.val; if (val != null) { if (retval.cached) { ret.add((T) val.getCurr()); } else { ret.add((T) val.get(_currTx)); } } else { ret.add(null); } } return ret; } @Override public List<T> multiUpdate(List<List<Object>> keys, List<ValueUpdater> updaters) { List<CachedBatchReadsMap.RetVal<OpaqueValue>> curr = _backing.multiGet(keys); List<OpaqueValue> newVals = new ArrayList<OpaqueValue>(curr.size()); List<T> ret = new ArrayList<T>(); for (int i = 0; i < curr.size(); i++) { CachedBatchReadsMap.RetVal<OpaqueValue> retval = curr.get(i); OpaqueValue<T> val = retval.val; ValueUpdater<T> updater = updaters.get(i); T prev; if (val == null) { prev = null; } else { if (retval.cached) { prev = val.getCurr(); } else { prev = val.get(_currTx); } } T newVal = updater.update(prev); ret.add(newVal); OpaqueValue<T> newOpaqueVal; if (val == null) { newOpaqueVal = new OpaqueValue<T>(_currTx, newVal); } else { newOpaqueVal = val.update(_currTx, newVal); } newVals.add(newOpaqueVal); } _backing.multiPut(keys, newVals); return ret; } @Override public void multiPut(List<List<Object>> keys, List<T> vals) { List<ValueUpdater> updaters = new ArrayList<ValueUpdater>(vals.size()); for (T val : vals) { updaters.add(new ReplaceUpdater<T>(val)); } multiUpdate(keys, updaters); } @Override public void beginCommit(Long txid) { _currTx = txid; _backing.reset(); } @Override public void commit(Long txid) { _currTx = null; _backing.reset(); } static class ReplaceUpdater<T> implements ValueUpdater<T> { T _t; public ReplaceUpdater(T t) { _t = t; } @Override public T update(Object stored) { return _t; } } }
state分三種類型,分別是non-transactional、transactional、opaque transactional,對應的spout也是這三種類型ui