Review Board 1.7.22


BasicTransactionSemantics should avoid throw-ing from close()

Review Request #10362 - Created April 9, 2013 and updated

Roshan Naik
flume-1.4
FLUME-1321
Reviewers
Flume
flume-git
Loggin error instead of throwing exception in cases where close() is called w/o rollback or abort being called.

 
flume-ng-core/src/main/java/org/apache/flume/channel/BasicTransactionSemantics.java
Revision 403cbca New Change
[20] 22 lines
[+20]
23
import org.apache.flume.ChannelException;
23
import org.apache.flume.ChannelException;
24
import org.apache.flume.Event;
24
import org.apache.flume.Event;
25
import org.apache.flume.Transaction;
25
import org.apache.flume.Transaction;
26

    
   
26

   
27
import com.google.common.base.Preconditions;
27
import com.google.common.base.Preconditions;

    
   
28
import org.slf4j.Logger;

    
   
29
import org.slf4j.LoggerFactory;
28

    
   
30

   
29
/**
31
/**
30
 * <p>
32
 * <p>
31
 * An implementation of basic {@link Transaction} semantics designed
33
 * An implementation of basic {@link Transaction} semantics designed
32
 * to work in concert with {@link BasicChannelSemantics} to simplify
34
 * to work in concert with {@link BasicChannelSemantics} to simplify
[+20] [20] 27 lines
[+20]
60
 */
62
 */
61
public abstract class BasicTransactionSemantics implements Transaction {
63
public abstract class BasicTransactionSemantics implements Transaction {
62

    
   
64

   
63
  private State state;
65
  private State state;
64
  private long initialThreadId;
66
  private long initialThreadId;

    
   
67
  private static Logger LOGGER = LoggerFactory.getLogger(BasicTransactionSemantics.class);
65

    
   
68

   
66
  protected void doBegin() throws InterruptedException {}
69
  protected void doBegin() throws InterruptedException {}
67
  protected abstract void doPut(Event event) throws InterruptedException;
70
  protected abstract void doPut(Event event) throws InterruptedException;
68
  protected abstract Event doTake() throws InterruptedException;
71
  protected abstract Event doTake() throws InterruptedException;
69
  protected abstract void doCommit() throws InterruptedException;
72
  protected abstract void doCommit() throws InterruptedException;
[+20] [20] 104 lines
[+20] [+] public void rollback() {
174

    
   
177

   
175
  @Override
178
  @Override
176
  public void close() {
179
  public void close() {
177
    Preconditions.checkState(Thread.currentThread().getId() == initialThreadId,
180
    Preconditions.checkState(Thread.currentThread().getId() == initialThreadId,
178
        "close() called from different thread than getTransaction()!");
181
        "close() called from different thread than getTransaction()!");
179
    Preconditions.checkState(
182
    if( state.equals(State.NEW) || state.equals(State.COMPLETED) )
180
            state.equals(State.NEW) || state.equals(State.COMPLETED),
183
       LOGGER.error("close() called when transaction is {} " +
181
            "close() called when transaction is %s"
184
              "- you must either commit or rollback first", state);
182
            + " - you must either commit or rollback first", state);

   
183

    
   
185

   
184
    state = State.CLOSED;
186
    state = State.CLOSED;
185
    doClose();
187
    doClose();
186
  }
188
  }
187

    
   
189

   
[+20] [20] 30 lines
  1. flume-ng-core/src/main/java/org/apache/flume/channel/BasicTransactionSemantics.java: Loading...