Open Kilda Java Documentation
ErrorBolt.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm.topology.flow.bolts;
17 
25 
26 import org.slf4j.LoggerFactory;
27 import org.slf4j.Logger;
28 import org.apache.storm.task.OutputCollector;
29 import org.apache.storm.task.TopologyContext;
30 import org.apache.storm.topology.OutputFieldsDeclarer;
31 import org.apache.storm.topology.base.BaseRichBolt;
32 import org.apache.storm.tuple.Tuple;
33 import org.apache.storm.tuple.Values;
34 
35 import java.util.Map;
36 
40 public class ErrorBolt extends BaseRichBolt {
44  private static final Logger logger = LoggerFactory.getLogger(ErrorBolt.class);
45 
49  private OutputCollector outputCollector;
50 
54  @Override
55  public void prepare(Map stormConf, TopologyContext context, OutputCollector outputCollector) {
56  this.outputCollector = outputCollector;
57  }
58 
62  @Override
63  public void execute(Tuple tuple) {
64  ComponentType componentId = ComponentType.valueOf(tuple.getSourceComponent());
65  StreamType streamId = StreamType.valueOf(tuple.getSourceStreamId());
66  ErrorType errorType = (ErrorType) tuple.getValueByField(FlowTopology.ERROR_TYPE_FIELD);
67  ErrorMessage error = (ErrorMessage) tuple.getValueByField(AbstractTopology.MESSAGE_FIELD);
69  Values values = new Values(error);
70 
71  try {
72  logger.debug("Request tuple={}", tuple);
73 
74  switch (componentId) {
75  case CRUD_BOLT:
76  case SPLITTER_BOLT:
77  logger.debug("Error message: data={}", error.getData());
78  outputCollector.emit(StreamType.RESPONSE.toString(), tuple, values);
79  break;
80  default:
81  logger.debug("Skip message from UNKNOWN component: component={}, stream={}, error-type={}",
82  componentId, streamId, errorType);
83  break;
84  }
85  } catch (Exception exception) {
86  logger.error("Could not process message: {}", tuple, exception);
87  } finally {
88  outputCollector.ack(tuple);
89 
90  logger.debug("Error message ack: component={}, stream={}, tuple={}, values={}",
91  tuple.getSourceComponent(), tuple.getSourceStreamId(), tuple, values);
92  }
93  }
94 
98  @Override
99  public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
100  outputFieldsDeclarer.declareStream(StreamType.RESPONSE.toString(), AbstractTopology.fieldMessage);
101  }
102 }
void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer)
Definition: ErrorBolt.java:99
void setDestination(final Destination destination)
Definition: Message.java:111
void prepare(Map stormConf, TopologyContext context, OutputCollector outputCollector)
Definition: ErrorBolt.java:55