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

    https://github.com/apache/storm/pull/1038#discussion_r50866045
  
    --- Diff: 
external/storm-mongodb/src/main/java/org/apache/storm/mongodb/trident/state/MongoState.java
 ---
    @@ -0,0 +1,97 @@
    +/**
    + * 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.mongodb.trident.state;
    +
    +import java.io.Serializable;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.apache.commons.lang.Validate;
    +import org.apache.storm.mongodb.common.MongoDBClient;
    +import org.apache.storm.mongodb.common.mapper.MongoMapper;
    +import org.apache.storm.trident.operation.TridentCollector;
    +import org.apache.storm.trident.state.State;
    +import org.apache.storm.trident.tuple.TridentTuple;
    +import org.bson.Document;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.collect.Lists;
    +
    +public class MongoState implements State {
    +
    +    private static final Logger LOG = 
LoggerFactory.getLogger(MongoState.class);
    +
    +    private Options options;
    +    private MongoDBClient mongoClient;
    +    private Map map;
    +
    +    protected MongoState(Map map, int partitionIndex, int numPartitions, 
Options options) {
    +        this.options = options;
    +        this.map = map;
    +    }
    +
    +    public static class Options implements Serializable {
    +        private String url;
    +        private String collectionName;
    +        private MongoMapper mapper;
    +
    +        public Options withUrl(String url) {
    +            this.url = url;
    +            return this;
    +        }
    +
    +        public Options withCollectionName(String collectionName) {
    +            this.collectionName = collectionName;
    +            return this;
    +        }
    +
    +        public Options withMapper(MongoMapper mapper) {
    +            this.mapper = mapper;
    +            return this;
    +        }
    +    }
    +
    +    protected void prepare() {
    +        Validate.notEmpty(options.url, "url can not be blank or null");
    +        Validate.notEmpty(options.collectionName, "collectionName can not 
be blank or null");
    +        Validate.notNull(options.mapper, "MongoMapper can not be null");
    +
    +        this.mongoClient = new MongoDBClient(options.url, 
options.collectionName);
    +    }
    +
    +    @Override
    +    public void beginCommit(Long txid) {
    +        LOG.debug("beginCommit is noop.");
    +    }
    +
    +    @Override
    +    public void commit(Long txid) {
    +        LOG.debug("commit is noop.");
    +    }
    +
    +    public void updateState(List<TridentTuple> tuples, TridentCollector 
collector) {
    +        List<Document> documents = Lists.newArrayList();
    +        for (TridentTuple tuple : tuples) {
    +            Document document = options.mapper.toDocument(tuple);
    +            documents.add(document);
    +        }
    +        this.mongoClient.insert(documents);
    --- End diff --
    
    The issue here is with failures.  I am not an mongodb expert so I don't 
know what happens if you insert in a document that is already there. If you 
have a distributed state there will be multiple instances of this class spear 
throughout your topology.  If a single tuple fails as part of the updateState 
trident will replay the entire batch.  In that case we can try to insert 
documents that were already inserted.  I am not worried about consistency, 
because there is no query possible from trident.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to