1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+ package org .apache .hadoop .fi ;
19+
20+ import java .util .ArrayList ;
21+ import java .util .List ;
22+
23+ import org .apache .hadoop .fi .FiTestUtil .Action ;
24+ import org .apache .hadoop .fi .FiTestUtil .ActionContainer ;
25+ import org .apache .hadoop .hdfs .protocol .DatanodeID ;
26+ import org .apache .hadoop .hdfs .protocol .DatanodeInfo ;
27+ import org .apache .hadoop .hdfs .protocol .LocatedBlock ;
28+ import org .apache .hadoop .hdfs .server .datanode .DataNode ;
29+ import org .apache .hadoop .util .DiskChecker .DiskOutOfSpaceException ;
30+
31+ /**
32+ * Utilities for DataTransferProtocol related tests,
33+ * e.g. TestFiDataTransferProtocol.
34+ */
35+ public class DataTransferTestUtil {
36+ private static DataTransferTest thepipelinetest ;
37+ /** initialize pipeline test */
38+ public static DataTransferTest initTest () {
39+ return thepipelinetest = new DataTransferTest ();
40+ }
41+ /** get the pipeline test object */
42+ public static DataTransferTest getPipelineTest () {
43+ return thepipelinetest ;
44+ }
45+
46+ /**
47+ * The DataTransferTest class includes a pipeline
48+ * and some actions.
49+ */
50+ public static class DataTransferTest {
51+ private Pipeline thepipeline ;
52+ /** Simulate action for the receiverOpWriteBlock pointcut */
53+ public final ActionContainer <DataNode > fiReceiverOpWriteBlock
54+ = new ActionContainer <DataNode >();
55+ /** Simulate action for the callReceivePacket pointcut */
56+ public final ActionContainer <DataNode > fiCallReceivePacket
57+ = new ActionContainer <DataNode >();
58+ /** Simulate action for the statusRead pointcut */
59+ public final ActionContainer <DataNode > fiStatusRead
60+ = new ActionContainer <DataNode >();
61+
62+ /** Initialize the pipeline. */
63+ public Pipeline initPipeline (LocatedBlock lb ) {
64+ if (thepipeline != null ) {
65+ throw new IllegalStateException ("thepipeline != null" );
66+ }
67+ return thepipeline = new Pipeline (lb );
68+ }
69+
70+ /** Return the pipeline. */
71+ public Pipeline getPipeline () {
72+ if (thepipeline == null ) {
73+ throw new IllegalStateException ("thepipeline == null" );
74+ }
75+ return thepipeline ;
76+ }
77+ }
78+
79+ /** A pipeline contains a list of datanodes. */
80+ public static class Pipeline {
81+ private final List <String > datanodes = new ArrayList <String >();
82+
83+ private Pipeline (LocatedBlock lb ) {
84+ for (DatanodeInfo d : lb .getLocations ()) {
85+ datanodes .add (d .getName ());
86+ }
87+ }
88+
89+ /** Does the pipeline contains d at the n th position? */
90+ public boolean contains (int n , DatanodeID d ) {
91+ return d .getName ().equals (datanodes .get (n ));
92+ }
93+
94+ /** {@inheritDoc} */
95+ public String toString () {
96+ return getClass ().getSimpleName () + datanodes ;
97+ }
98+ }
99+
100+ /** Action for DataNode */
101+ public static abstract class DataNodeAction implements Action <DataNode > {
102+ /** The name of the test */
103+ final String currentTest ;
104+ /** The index of the datanode */
105+ final int index ;
106+
107+ /**
108+ * @param currentTest The name of the test
109+ * @param index The index of the datanode
110+ */
111+ private DataNodeAction (String currentTest , int index ) {
112+ this .currentTest = currentTest ;
113+ this .index = index ;
114+ }
115+
116+ /** {@inheritDoc} */
117+ public String toString () {
118+ return currentTest + ", index=" + index ;
119+ }
120+
121+ /** {@inheritDoc} */
122+ String toString (DataNode datanode ) {
123+ return "FI: " + this + ", datanode="
124+ + datanode .getDatanodeRegistration ().getName ();
125+ }
126+ }
127+
128+ /** Throws OutOfMemoryError. */
129+ public static class OomAction extends DataNodeAction {
130+ /** Create an action for datanode i in the pipeline. */
131+ public OomAction (String currentTest , int i ) {
132+ super (currentTest , i );
133+ }
134+
135+ @ Override
136+ public void run (DataNode datanode ) {
137+ final Pipeline p = getPipelineTest ().getPipeline ();
138+ if (p .contains (index , datanode .getDatanodeRegistration ())) {
139+ final String s = toString (datanode );
140+ FiTestUtil .LOG .info (s );
141+ throw new OutOfMemoryError (s );
142+ }
143+ }
144+ }
145+
146+ /** Throws DiskOutOfSpaceException. */
147+ public static class DoosAction extends DataNodeAction {
148+ /** Create an action for datanode i in the pipeline. */
149+ public DoosAction (String currentTest , int i ) {
150+ super (currentTest , i );
151+ }
152+
153+ @ Override
154+ public void run (DataNode datanode ) throws DiskOutOfSpaceException {
155+ final Pipeline p = getPipelineTest ().getPipeline ();
156+ if (p .contains (index , datanode .getDatanodeRegistration ())) {
157+ final String s = toString (datanode );
158+ FiTestUtil .LOG .info (s );
159+ throw new DiskOutOfSpaceException (s );
160+ }
161+ }
162+ }
163+
164+ /**
165+ * Sleep some period of time so that it slows down the datanode
166+ * or sleep forever so that datanode becomes not responding.
167+ */
168+ public static class SleepAction extends DataNodeAction {
169+ /** In milliseconds, duration <= 0 means sleeping forever.*/
170+ final long duration ;
171+
172+ /**
173+ * Create an action for datanode i in the pipeline.
174+ * @param duration In milliseconds, duration <= 0 means sleeping forever.
175+ */
176+ public SleepAction (String currentTest , int i , long duration ) {
177+ super (currentTest , i );
178+ this .duration = duration ;
179+ }
180+
181+ @ Override
182+ public void run (DataNode datanode ) {
183+ final Pipeline p = getPipelineTest ().getPipeline ();
184+ if (p .contains (index , datanode .getDatanodeRegistration ())) {
185+ final String s = toString (datanode ) + ", duration=" + duration ;
186+ FiTestUtil .LOG .info (s );
187+ if (duration <= 0 ) {
188+ for (; true ; FiTestUtil .sleep (1000 )); //sleep forever
189+ } else {
190+ FiTestUtil .sleep (duration );
191+ }
192+ }
193+ }
194+ }
195+ }
0 commit comments