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
19 package org.apache.hadoop.chukwa.datacollection.writer;
20
21
22 import java.util.List;
23 import java.util.ArrayList;
24 import org.apache.hadoop.chukwa.Chunk;
25 import org.apache.hadoop.conf.Configuration;
26
27 public interface ChukwaWriter {
28
29 public static abstract class CommitStatus {}
30
31 public static final CommitStatus COMMIT_OK = new CommitStatus() {};
32 public static final CommitStatus COMMIT_FAIL = new CommitStatus() {};
33
34 /**
35 * COMMIT_PENDING should be returned if a writer has written data, but
36 * this data may ultimately disappear. Contains a list of strings, format
37 * unspecified, that agents can use to find out, eventually, if their data
38 * has committed. String <n> corresponds to the nth chunk passed to add().
39 *
40 * At present, the format is <sinkfilename> <offset>,
41 * where sinkfilename is the name of a sinkfile, without directory but with
42 * .done suffix, and offset is the last byte of the associated chunk.
43 */
44 public static class COMMIT_PENDING extends CommitStatus {
45 public List<String> pendingEntries;
46
47 public COMMIT_PENDING(int entries) {
48 pendingEntries = new ArrayList<String>(entries);
49 }
50
51 public void addPend(String currentFileName, long dataSize) {
52 pendingEntries.add(currentFileName+ " " + dataSize+"\n");
53 }
54 }
55
56 /**
57 * Called once to initialize this writer.
58 *
59 * @param c
60 * @throws WriterException
61 */
62 public void init(Configuration c) throws WriterException;
63
64 /**
65 * Called repeatedly with data that should be serialized.
66 *
67 * Subclasses may assume that init() will be called before any calls to
68 * add(), and that add() won't be called after close().
69 *
70 * @param chunks
71 * @return
72 * @throws WriterException
73 */
74 public CommitStatus add(List<Chunk> chunks) throws WriterException;
75
76 /**
77 * Called once, indicating that the writer should close files and prepare
78 * to exit.
79 * @throws WriterException
80 */
81 public void close() throws WriterException;
82
83 }