在flume作为通道接收json数据时,最近遇到一个问题,当flume-es-sink遭遇一个错误的时候,会不断尝试插入数据,而以前的数据又没有进行回滚,导致数据重复插入,脏数据累积,为了解决这个问题,现解决如下:
原因如下:
1,事务控制在channel端
2,事务回滚,未处理已插入es中数据
解决方案:
1,es批量操作不做回滚
2,es插入出错,只做报警(日志业务,不要求强事务)
总结:
在解决问题的过程中,解决问题的关键在于两点:
1,分析错误数据
2,关键点日志分析
找到这两个点,解决问题基本就可以定位,在以后的工作生涯中,我们要沉住气,不要怕,为了以后不犯错,保留犯错现场。
修改flume源码如下
关键代码:
} catch (Throwable ex) {
logger.error("=ElasticSearchSink=>has error",ex);
try {
txn.commit();
sinkCounter.addToEventDrainSuccessCount(0);
counterGroup.incrementAndGet("transaction.success");
} catch (Exception ex2) {
logger.error(
"=ElasticSearchSink.counterGroup.incrementAndGet=> has exception.",
ex2);
}
}
elasticsearch-sink:
/*
* 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.flume.sink.elasticsearch;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import org.apache.commons.lang.StringUtils;
import org.apache.