Package com.cloudera.flume.core

Source Code of com.cloudera.flume.core.TestFailOverSink

/**
* Licensed to Cloudera, Inc. under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  Cloudera, Inc. 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 com.cloudera.flume.core;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

import com.cloudera.flume.handlers.debug.IntervalFlakeyEventSink;
import com.cloudera.flume.reporter.aggregator.CounterSink;

/**
* This tests the failover pipe mechanism. The semantics of this is that it
* attempts to write to the primary, and if it failes with an exception, it
* fails over to the secondary. If the secondary fails as well, an exception is
* thrown.
*/
public class TestFailOverSink {

  @Test
  public void testFailOverSink() throws IOException, InterruptedException {
    CounterSink primary = new CounterSink("primary");
    CounterSink secondary = new CounterSink("backup");

    IntervalFlakeyEventSink<EventSink> flake = new IntervalFlakeyEventSink<EventSink>(
        primary, 2);
    FailOverSink failsink = new FailOverSink(flake, secondary);
    failsink.open();
    for (int i = 0; i < 100; i++) {
      Event e = new EventImpl(("event " + i).getBytes());
      failsink.append(e);
    }

    // this should succeed, with the counts being equal in primary and
    // secondary.
    failsink.close();
    Assert.assertEquals(primary.getCount(), secondary.getCount());
  }

  /**
   * This does multiple levels of failover.
   *
   * @throws InterruptedException
   */
  @Test
  public void testMultiFailOverSink() throws IOException, InterruptedException {
    CounterSink primary = new CounterSink("primary");
    CounterSink secondary = new CounterSink("backup");
    CounterSink tertiary = new CounterSink("tertiary");

    IntervalFlakeyEventSink<EventSink> flake1 = new IntervalFlakeyEventSink<EventSink>(
        primary, 2);
    IntervalFlakeyEventSink<EventSink> flake2 = new IntervalFlakeyEventSink<EventSink>(
        secondary, 2);
    FailOverSink failsink0 = new FailOverSink(flake2, tertiary);
    FailOverSink failsink = new FailOverSink(flake1, failsink0);
    failsink.open();
    for (int i = 0; i < 100; i++) {
      Event e = new EventImpl(("event " + i).getBytes());
      failsink.append(e);
    }

    // this should succeed, with the counts being equal in primary and
    // secondary, and tertiary.
    failsink.close();
    Assert.assertEquals(50, primary.getCount());
    Assert.assertEquals(25, secondary.getCount());
    Assert.assertEquals(25, tertiary.getCount());

  }
}
TOP

Related Classes of com.cloudera.flume.core.TestFailOverSink

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.