Skip to content

Commit 896f9e3

Browse files
mehakmeetdeepakdamri
authored andcommitted
HADOOP-17461. Collect thread-level IOStatistics. (apache#4352)
This adds a thread-level collector of IOStatistics, IOStatisticsContext, which can be: * Retrieved for a thread and cached for access from other threads. * reset() to record new statistics. * Queried for live statistics through the IOStatisticsSource.getIOStatistics() method. * Queries for a statistics aggregator for use in instrumented classes. * Asked to create a serializable copy in snapshot() The goal is to make it possible for applications with multiple threads performing different work items simultaneously to be able to collect statistics on the individual threads, and so generate aggregate reports on the total work performed for a specific job, query or similar unit of work. Some changes in IOStatistics-gathering classes are needed for this feature * Caching the active context's aggregator in the object's constructor * Updating it in close() Slightly more work is needed in multithreaded code, such as the S3A committers, which collect statistics across all threads used in task and job commit operations. Currently the IOStatisticsContext-aware classes are: * The S3A input stream, output stream and list iterators. * RawLocalFileSystem's input and output streams. * The S3A committers. * The TaskPool class in hadoop-common, which propagates the active context into scheduled worker threads. Collection of statistics in the IOStatisticsContext is disabled process-wide by default until the feature is considered stable. To enable the collection, set the option fs.thread.level.iostatistics.enabled to "true" in core-site.xml; Contributed by Mehakmeet Singh and Steve Loughran
1 parent f3713e1 commit 896f9e3

File tree

7 files changed

+1021
-3
lines changed

7 files changed

+1021
-3
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.fs.statistics;
20+
21+
import org.apache.hadoop.fs.statistics.impl.IOStatisticsContextIntegration;
22+
23+
/**
24+
* An interface defined to capture thread-level IOStatistics by using per
25+
* thread context.
26+
* <p>
27+
* The aggregator should be collected in their constructor by statistics-generating
28+
* classes to obtain the aggregator to update <i>across all threads</i>.
29+
* <p>
30+
* The {@link #snapshot()} call creates a snapshot of the statistics;
31+
* <p>
32+
* The {@link #reset()} call resets the statistics in the context so
33+
* that later snapshots will get the incremental data.
34+
*/
35+
public interface IOStatisticsContext extends IOStatisticsSource {
36+
37+
/**
38+
* Get the IOStatisticsAggregator for the context.
39+
*
40+
* @return return the aggregator for the context.
41+
*/
42+
IOStatisticsAggregator getAggregator();
43+
44+
/**
45+
* Capture the snapshot of the context's IOStatistics.
46+
*
47+
* @return IOStatisticsSnapshot for the context.
48+
*/
49+
IOStatisticsSnapshot snapshot();
50+
51+
/**
52+
* Get a unique ID for this context, for logging
53+
* purposes.
54+
*
55+
* @return an ID unique for all contexts in this process.
56+
*/
57+
long getID();
58+
59+
/**
60+
* Reset the context's IOStatistics.
61+
*/
62+
void reset();
63+
64+
/**
65+
* Get the context's IOStatisticsContext.
66+
*
67+
* @return instance of IOStatisticsContext for the context.
68+
*/
69+
static IOStatisticsContext getCurrentIOStatisticsContext() {
70+
return IOStatisticsContextIntegration.getCurrentIOStatisticsContext();
71+
}
72+
73+
/**
74+
* Set the IOStatisticsContext for the current thread.
75+
* @param statisticsContext IOStatistics context instance for the
76+
* current thread. If null, the context is reset.
77+
*/
78+
static void setThreadIOStatisticsContext(
79+
IOStatisticsContext statisticsContext) {
80+
IOStatisticsContextIntegration.setThreadIOStatisticsContext(
81+
statisticsContext);
82+
}
83+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.fs.statistics.impl;
20+
21+
import org.apache.hadoop.fs.statistics.IOStatistics;
22+
import org.apache.hadoop.fs.statistics.IOStatisticsAggregator;
23+
import org.apache.hadoop.fs.statistics.IOStatisticsContext;
24+
import org.apache.hadoop.fs.statistics.IOStatisticsSnapshot;
25+
26+
/**
27+
* Empty IOStatistics context which serves no-op for all the operations and
28+
* returns an empty Snapshot if asked.
29+
*
30+
*/
31+
final class EmptyIOStatisticsContextImpl implements IOStatisticsContext {
32+
33+
private static final IOStatisticsContext EMPTY_CONTEXT = new EmptyIOStatisticsContextImpl();
34+
35+
private EmptyIOStatisticsContextImpl() {
36+
}
37+
38+
/**
39+
* Create a new empty snapshot.
40+
* A new one is always created for isolation.
41+
*
42+
* @return a statistics snapshot
43+
*/
44+
@Override
45+
public IOStatisticsSnapshot snapshot() {
46+
return new IOStatisticsSnapshot();
47+
}
48+
49+
@Override
50+
public IOStatisticsAggregator getAggregator() {
51+
return EmptyIOStatisticsStore.getInstance();
52+
}
53+
54+
@Override
55+
public IOStatistics getIOStatistics() {
56+
return EmptyIOStatistics.getInstance();
57+
}
58+
59+
@Override
60+
public void reset() {}
61+
62+
/**
63+
* The ID is always 0.
64+
* As the real context implementation counter starts at 1,
65+
* we are guaranteed to have unique IDs even between them and
66+
* the empty context.
67+
* @return 0
68+
*/
69+
@Override
70+
public long getID() {
71+
return 0;
72+
}
73+
74+
/**
75+
* Get the single instance.
76+
* @return an instance.
77+
*/
78+
static IOStatisticsContext getInstance() {
79+
return EMPTY_CONTEXT;
80+
}
81+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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.fs.statistics.impl;
20+
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
import org.apache.hadoop.fs.statistics.IOStatistics;
25+
import org.apache.hadoop.fs.statistics.IOStatisticsAggregator;
26+
import org.apache.hadoop.fs.statistics.IOStatisticsContext;
27+
import org.apache.hadoop.fs.statistics.IOStatisticsSnapshot;
28+
29+
/**
30+
* Implementing the IOStatisticsContext.
31+
*
32+
* A Context defined for IOStatistics collection per thread which captures
33+
* each worker thread's work in FS streams and stores it in the form of
34+
* IOStatisticsSnapshot.
35+
*
36+
* For the current thread the IOStatisticsSnapshot can be used as a way to
37+
* move the IOStatistics data between applications using the Serializable
38+
* nature of the class.
39+
*/
40+
public final class IOStatisticsContextImpl implements IOStatisticsContext {
41+
private static final Logger LOG =
42+
LoggerFactory.getLogger(IOStatisticsContextImpl.class);
43+
44+
/**
45+
* Thread ID.
46+
*/
47+
private final long threadId;
48+
49+
/**
50+
* Unique ID.
51+
*/
52+
private final long id;
53+
54+
/**
55+
* IOStatistics to aggregate.
56+
*/
57+
private final IOStatisticsSnapshot ioStatistics = new IOStatisticsSnapshot();
58+
59+
/**
60+
* Constructor.
61+
* @param threadId thread ID
62+
* @param id instance ID.
63+
*/
64+
public IOStatisticsContextImpl(final long threadId, final long id) {
65+
this.threadId = threadId;
66+
this.id = id;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return "IOStatisticsContextImpl{" +
72+
"id=" + id +
73+
", threadId=" + threadId +
74+
", ioStatistics=" + ioStatistics +
75+
'}';
76+
}
77+
78+
/**
79+
* Get the IOStatisticsAggregator of the context.
80+
* @return the instance of IOStatisticsAggregator for this context.
81+
*/
82+
@Override
83+
public IOStatisticsAggregator getAggregator() {
84+
return ioStatistics;
85+
}
86+
87+
/**
88+
* Returns a snapshot of the current thread's IOStatistics.
89+
*
90+
* @return IOStatisticsSnapshot of the context.
91+
*/
92+
@Override
93+
public IOStatisticsSnapshot snapshot() {
94+
LOG.debug("Taking snapshot of IOStatisticsContext id {}", id);
95+
return new IOStatisticsSnapshot(ioStatistics);
96+
}
97+
98+
/**
99+
* Reset the thread +.
100+
*/
101+
@Override
102+
public void reset() {
103+
LOG.debug("clearing IOStatisticsContext id {}", id);
104+
ioStatistics.clear();
105+
}
106+
107+
@Override
108+
public IOStatistics getIOStatistics() {
109+
return ioStatistics;
110+
}
111+
112+
/**
113+
* ID of this context.
114+
* @return ID.
115+
*/
116+
@Override
117+
public long getID() {
118+
return id;
119+
}
120+
121+
/**
122+
* Get the thread ID.
123+
* @return thread ID.
124+
*/
125+
public long getThreadID() {
126+
return threadId;
127+
}
128+
}

0 commit comments

Comments
 (0)