You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@storm.apache.org by HeartSaVioR <gi...@git.apache.org> on 2017/04/01 12:31:46 UTC

[GitHub] storm pull request #1970: STORM-2383 Support HBase as state backend (1.x)

Github user HeartSaVioR commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1970#discussion_r109284284
  
    --- Diff: external/storm-hbase/src/main/java/org/apache/storm/hbase/state/HBaseKeyValueState.java ---
    @@ -0,0 +1,399 @@
    +/*
    + * 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.storm.hbase.state;
    +
    +import org.apache.hadoop.hbase.client.Delete;
    +import org.apache.hadoop.hbase.client.Durability;
    +import org.apache.hadoop.hbase.client.Get;
    +import org.apache.hadoop.hbase.client.Mutation;
    +import org.apache.hadoop.hbase.client.Result;
    +import org.apache.storm.hbase.bolt.mapper.HBaseProjectionCriteria;
    +import org.apache.storm.hbase.common.ColumnList;
    +import org.apache.storm.hbase.common.HBaseClient;
    +import org.apache.storm.hbase.utils.HBaseEncoder;
    +import org.apache.storm.state.DefaultStateSerializer;
    +import org.apache.storm.state.KeyValueState;
    +import org.apache.storm.state.Serializer;
    +import org.apache.storm.utils.ByteArrayUtil;
    +import org.apache.storm.utils.ByteArrayWrapper;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.util.Arrays;
    +import java.util.Collections;
    +import java.util.HashMap;
    +import java.util.Iterator;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.NavigableMap;
    +import java.util.concurrent.ConcurrentHashMap;
    +
    +/**
    + * A Hbase based implementation that persists the state in HBase.
    + */
    +public class HBaseKeyValueState<K, V> implements KeyValueState<K, V> {
    +    private static final Logger LOG = LoggerFactory.getLogger(HBaseKeyValueState.class);
    +
    +    private static final ByteArrayWrapper COMMIT_TXID_KEY = new ByteArrayWrapper("commit".getBytes());
    +    private static final ByteArrayWrapper PREPARE_TXID_KEY = new ByteArrayWrapper("prepare".getBytes());
    +
    +    private final byte[] namespace;
    +    private final byte[] prepareNamespace;
    +    private final byte[] txidNamespace;
    +    private final byte[] columnFamily;
    +    private final HBaseEncoder<K, V> encoder;
    +    private final HBaseClient hBaseClient;
    +
    +    private Map<ByteArrayWrapper, byte[]> pendingPrepare;
    +    private Map<ByteArrayWrapper, byte[]> pendingCommit;
    +
    +    // the key and value of txIds are guaranteed to be converted to UTF-8 encoded String
    +    private Map<ByteArrayWrapper, byte[]> txIds;
    +
    +    public HBaseKeyValueState(HBaseClient hbaseClient, String columnFamily, String namespace) {
    +        this(hbaseClient, columnFamily, namespace, new DefaultStateSerializer<K>(), new DefaultStateSerializer<V>());
    +    }
    +
    +    public HBaseKeyValueState(HBaseClient hBaseClient, String columnFamily, String namespace,
    +                              Serializer<K> keySerializer, Serializer<V> valueSerializer) {
    +
    +        this.hBaseClient = hBaseClient;
    +        this.columnFamily = columnFamily.getBytes();
    +        this.namespace = namespace.getBytes();
    +        this.prepareNamespace = (namespace + "$prepare").getBytes();
    +        this.txidNamespace = (namespace + "$txid").getBytes();
    +        this.encoder = new HBaseEncoder<K, V>(keySerializer, valueSerializer);
    +        this.pendingPrepare = createPendingPrepareMap();
    +        initTxids();
    +        initPendingCommit();
    +    }
    +
    +    private void initTxids() {
    +        HBaseProjectionCriteria criteria = new HBaseProjectionCriteria();
    +        criteria.addColumnFamily(columnFamily);
    +        Get get = hBaseClient.constructGetRequests(txidNamespace, criteria);
    +        try {
    +            Result[] results = hBaseClient.batchGet(Collections.singletonList(get));
    +            Result result = results[0];
    +            if (!result.isEmpty()) {
    +                NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(columnFamily);
    +                txIds = ByteArrayUtil.Maps.newHashMapWrappingKey(familyMap);
    +            } else {
    +                txIds = new HashMap<>();
    +            }
    +
    +            LOG.debug("initTxids, txIds {}", txIds);
    +        } catch (Exception e) {
    +            throw new RuntimeException(e);
    +        }
    +    }
    +
    +    private void initPendingCommit() {
    +        HBaseProjectionCriteria criteria = new HBaseProjectionCriteria();
    +        criteria.addColumnFamily(columnFamily);
    +        Get get = hBaseClient.constructGetRequests(prepareNamespace, criteria);
    +        try {
    +            Result[] results = hBaseClient.batchGet(Collections.singletonList(get));
    +            Result result = results[0];
    +            if (!result.isEmpty()) {
    +                LOG.debug("Loading previously prepared commit from {}", prepareNamespace);
    +                NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(columnFamily);
    +                pendingCommit = ByteArrayUtil.Maps.newHashMapWrappingKey(familyMap);
    +            } else {
    +                LOG.debug("No previously prepared commits.");
    +                pendingCommit = Collections.emptyMap();
    +            }
    +        } catch (Exception e) {
    +            throw new RuntimeException(e);
    +        }
    +    }
    +
    +    @Override
    +    public void put(K key, V value) {
    +        LOG.debug("put key '{}', value '{}'", key, value);
    +        byte[] columnKey = encoder.encodeKey(key);
    +        byte[] columnValue = encoder.encodeValue(value);
    +        pendingPrepare.put(new ByteArrayWrapper(columnKey), columnValue);
    +    }
    +
    +    @Override
    +    public V get(K key) {
    +        LOG.debug("get key '{}'", key);
    +        byte[] columnKey = encoder.encodeKey(key);
    +        byte[] columnValue = null;
    +
    +        ByteArrayWrapper wrappedKey = new ByteArrayWrapper(columnKey);
    +        if (pendingPrepare.containsKey(wrappedKey)) {
    +            columnValue = pendingPrepare.get(wrappedKey);
    +        } else if (pendingCommit.containsKey(wrappedKey)) {
    +            columnValue = pendingCommit.get(wrappedKey);
    +        } else {
    +            HBaseProjectionCriteria criteria = new HBaseProjectionCriteria();
    +            HBaseProjectionCriteria.ColumnMetaData column = new HBaseProjectionCriteria.ColumnMetaData(columnFamily, columnKey);
    +            criteria.addColumn(column);
    +            Get get = hBaseClient.constructGetRequests(namespace, criteria);
    +            try {
    +                Result[] results = hBaseClient.batchGet(Collections.singletonList(get));
    +                Result result = results[0];
    +                columnValue = result.getValue(column.getColumnFamily(), column.getQualifier());
    +            } catch (Exception e) {
    +                throw new RuntimeException(e);
    +            }
    +        }
    +        V value = null;
    +        if (columnValue != null) {
    +            value = encoder.decodeValue(columnValue);
    +        }
    +        LOG.debug("Value for key '{}' is '{}'", key, value);
    +        return value;
    +    }
    +
    +    @Override
    +    public V get(K key, V defaultValue) {
    +        V val = get(key);
    +        return val != null ? val : defaultValue;
    +    }
    +
    +    @Override
    +    public V delete(K key) {
    +        LOG.debug("delete key '{}'", key);
    +        byte[] columnKey = encoder.encodeKey(key);
    +        V curr = get(key);
    +        pendingPrepare.put(new ByteArrayWrapper(columnKey), HBaseEncoder.TOMBSTONE);
    +        return curr;
    +    }
    +
    +    @Override
    +    public Iterator<Map.Entry<K, V>> iterator() {
    +        // TODO: is it possible to implement this correctly?
    --- End diff --
    
    OK. Filed [STORM-2449](https://issues.apache.org/jira/browse/STORM-2449).
    I'll apply the approach to this PR, too.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---