summaryrefslogtreecommitdiffstats
path: root/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/HdfsService.java
blob: 135a2c09ac52e2750f45e90d2ccc204e1f98f9fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* ============LICENSE_START=======================================================
* ONAP : DATALAKE
* ================================================================================
* Copyright 2019 China Mobile
*=================================================================================
* Licensed 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.
* ============LICENSE_END=========================================================
*/

package org.onap.datalake.feeder.service;

import java.io.IOException;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.onap.datalake.feeder.config.ApplicationConfiguration;
import org.onap.datalake.feeder.domain.Db;
import org.onap.datalake.feeder.dto.TopicConfig;
import org.onap.datalake.feeder.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.Getter;
import lombok.Setter;

/**
 * Service to write data to HDFS
 * 
 * @author Guobiao Mo
 *
 */
@Service
public class HdfsService {

	private final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	ApplicationConfiguration config;

	@Autowired
	private DbService dbService;

	FileSystem fileSystem;
	private boolean isReady = false;

	private ThreadLocal<Map<String, Buffer>> bufferLocal = ThreadLocal.withInitial(HashMap::new);
	private ThreadLocal<SimpleDateFormat> dayFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
	private ThreadLocal<SimpleDateFormat> timeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS"));

	@Setter
	@Getter
	private class Buffer {
		long lastFlush;
		List<String> data;

		public Buffer() {
			lastFlush = Long.MIN_VALUE;
			data = new ArrayList<>();
		}

		public void flush(String topic) {
			try {
				if (!data.isEmpty()) {
					saveMessages(topic, data);
					data.clear();
					lastFlush = System.currentTimeMillis();
				}
			} catch (IOException e) {
				log.error("error saving to HDFS." + topic, e);
			}
		}

		public void flushStall(String topic) {
			if (!data.isEmpty() && Util.isStall(lastFlush, config.getHdfsFlushInterval())) {
				log.debug("going to flushStall topic={}, buffer size={}", topic, data.size());
				flush(topic);
			}
		}

		public void addData(List<Pair<Long, String>> messages) {
			if (data.isEmpty()) { //reset the last flush time stamp to current if no existing data in buffer
				lastFlush = System.currentTimeMillis();
			}

			messages.stream().forEach(message -> data.add(message.getRight()));//note that message left is not used			
		}

		private void saveMessages(String topic, List<String> bufferList) throws IOException {

			long thread = Thread.currentThread().getId();
			Date date = new Date();
			String day = dayFormat.get().format(date);
			String time = timeFormat.get().format(date);

			InetAddress inetAddress = InetAddress.getLocalHost();
			String hostName = inetAddress.getHostName();

			String filePath = String.format("/datalake/%s/%s/%s-%s-%s", topic, day, time, hostName, thread);
			Path path = new Path(filePath);
			log.debug("writing {} to HDFS {}", bufferList.size(), filePath);

			// Create a new file and write data to it.
			FSDataOutputStream out = fileSystem.create(path, true, config.getHdfsBufferSize());

			bufferList.stream().forEach(message -> {
				try {
					out.writeUTF(message);
					out.write('\n');
				} catch (IOException e) {
					log.error("error writing to HDFS.", e);
				}
			});

			out.close();
		}
	}

	@PostConstruct
	private void init() {
		// Initialize HDFS Connection 
		try {
			Db hdfs = dbService.getHdfs();

			//Get configuration of Hadoop system
			Configuration hdfsConfig = new Configuration();

			int port = hdfs.getPort() == null ? 8020 : hdfs.getPort();

			String hdfsuri = String.format("hdfs://%s:%s", hdfs.getHost(), port);
			hdfsConfig.set("fs.defaultFS", hdfsuri);
			System.setProperty("HADOOP_USER_NAME", hdfs.getLogin());

			log.info("Connecting to -- {} as {}", hdfsuri, hdfs.getLogin());

			fileSystem = FileSystem.get(hdfsConfig);

			isReady = true;
		} catch (Exception ex) {
			log.error("error connection to HDFS.", ex);
			isReady = false;
		}
	}

	@PreDestroy
	public void cleanUp() {
		try {
			flush();
			fileSystem.close();
		} catch (IOException e) {
			log.error("fileSystem.close() at cleanUp.", e);
		}
	}

	public void flush() {
		bufferLocal.get().forEach((topic, buffer) -> buffer.flush(topic));
	}

	//if no new data comes in for a topic for a while, need to flush its buffer
	public void flushStall() {
		bufferLocal.get().forEach((topic, buffer) -> buffer.flushStall(topic));
	}

	public void saveMessages(TopicConfig topic, List<Pair<Long, String>> messages) {
		String topicStr = topic.getName();

		Map<String, Buffer> bufferMap = bufferLocal.get();
		final Buffer buffer = bufferMap.computeIfAbsent(topicStr, k -> new Buffer());

		buffer.addData(messages);

		if (!config.isAsync() || buffer.getData().size() >= config.getHdfsBatchSize()) {
			buffer.flush(topicStr);
		} else {
			log.debug("buffer size too small to flush: bufferData.size() {} < config.getHdfsBatchSize() {}", buffer.getData().size(), config.getHdfsBatchSize());
		}
	}

}