/*
 * Copyright 2012 Database Lab., Korea Univ.
 *
 * 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 tajo.engine.cluster;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.net.NetUtils;
import tajo.QueryUnitAttemptId;
import tajo.common.Sleeper;
import tajo.conf.TajoConf;
import tajo.conf.TajoConf.ConfVars;
import tajo.engine.MasterWorkerProtos.StatusReportProto;
import tajo.engine.MasterWorkerProtos.TaskStatusProto;
import tajo.engine.planner.global.QueryUnitAttempt;
import tajo.engine.query.StatusReportImpl;
import tajo.ipc.MasterWorkerProtocol;
import tajo.ipc.StatusReport;
import tajo.master.TajoMaster;
import tajo.rpc.NettyRpc;
import tajo.rpc.NettyRpcServer;
import tajo.rpc.protocolrecords.PrimitiveProtos.BoolProto;

import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicInteger;

public class WorkerListener extends Thread implements MasterWorkerProtocol {
  
  private final static Log LOG = LogFactory.getLog(WorkerListener.class);
  private final NettyRpcServer rpcServer;
  private InetSocketAddress bindAddr;
  private String addr;
  private volatile boolean stopped = false;
  private QueryManager qm;
  private TajoMaster master;
  private AtomicInteger processed;
  private Sleeper sleeper;
  
  public WorkerListener(TajoConf conf, QueryManager qm, TajoMaster master) {
    String confMasterAddr = conf.getVar(ConfVars.MASTER_ADDRESS);
    InetSocketAddress initIsa = NetUtils.createSocketAddr(confMasterAddr);
    if (initIsa.getAddress() == null) {
      throw new IllegalArgumentException("Failed resolve of " + initIsa);
    }
    this.rpcServer = NettyRpc.getProtoParamRpcServer(this, 
        MasterWorkerProtocol.class, initIsa);
    this.qm = qm;
    this.stopped = false;
    this.rpcServer.start();
    this.bindAddr = rpcServer.getBindAddress();
    this.addr = bindAddr.getHostName() + ":" + bindAddr.getPort();
    processed = new AtomicInteger(0);
    sleeper = new Sleeper();
    this.master = master;
  }
  
  public InetSocketAddress getBindAddress() {
    return this.bindAddr;
  }
  
  public String getAddress() {
    return this.addr;
  }
  
  public boolean isStopped() {
    return this.stopped;
  }
  
  public void shutdown() {
    this.stopped = true;
  }

  static BoolProto TRUE_PROTO = BoolProto.newBuilder().setValue(true).build();

  @Override
  public BoolProto statusUpdate(StatusReportProto proto) {

    if (master.getClusterManager().getFailedWorkers().contains(
        proto.getServerName())) {
      LOG.info("**** Dead man alive!!!!!!");
    }

    StatusReport report = new StatusReportImpl(proto);
    for (TaskStatusProto status : report.getProgressList()) {
      QueryUnitAttemptId uid = new QueryUnitAttemptId(status.getId());
      //qm.updateProgress(uid, status);
      QueryUnitAttempt attempt = qm.getQueryUnitAttempt(uid);
      attempt.updateProgress(status);
      processed.incrementAndGet();
    }

    return TRUE_PROTO;
  }

  @Override
  public BoolProto ping(QueryUnitAttemptId taskId) {
    LOG.info("Ping is Called!");
    return TRUE_PROTO;
  }

  @Override
  public void run() {
    // rpc listen
    try {
      while (!this.stopped) {
        processed.set(0);
        sleeper.sleep(1000);
        LOG.info("processed: " + processed);
      }
    } catch (InterruptedException e) {
      LOG.error(ExceptionUtils.getFullStackTrace(e));
    } finally {
      rpcServer.shutdown();
    }
  }
}

@@ -3364,46 +3364,54 @@
     
-LOG.info(%22**** Dead man alive!!!!!!%22);
+// TODO: remove the sender from failed workers
 %0A   
@@ -3881,41 +3881,8 @@
 ) %7B%0A
-    LOG.info(%22Ping is Called!%22);%0A
     
@@ -4056,53 +4056,8 @@
 0);%0A
-        LOG.info(%22processed: %22 + processed);%0A
     

Remove unnecessary logs from the WorkerListener