/
StagingManager.java
189 lines (173 loc) · 7.43 KB
/
StagingManager.java
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
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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 org.jumpmind.symmetric.io.stage;
import java.io.File;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.io.stage.IStagedResource.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StagingManager implements IStagingManager {
protected static final Logger log = LoggerFactory.getLogger(StagingManager.class);
protected File directory;
protected Map<String, IStagedResource> resourceList = new ConcurrentHashMap<String, IStagedResource>();
public StagingManager(String directory) {
log.info("The staging directory was initialized at the following location: " + directory);
this.directory = new File(directory);
this.directory.mkdirs();
refreshResourceList();
}
public Collection<String> getResourceReferences() {
synchronized (StagingManager.class) {
return resourceList.keySet();
}
}
protected void refreshResourceList() {
synchronized (StagingManager.class) {
Collection<File> files = FileUtils.listFiles(this.directory,
new String[] { State.CREATE.getExtensionName(), State.READY.getExtensionName(),
State.DONE.getExtensionName() }, true);
for (File file : files) {
try {
StagedResource resource = new StagedResource(0, directory,
file, this);
String path = resource.getPath();
if (!resourceList.containsKey(path)) {
resourceList.put(path, resource);
}
} catch (IllegalStateException ex) {
log.warn(ex.getMessage());
}
}
}
}
/**
* Clean up resources that are older than the passed in parameter.
*
* @param ttlInMs
* If resources are older than this number of milliseconds they
* will be purged
*/
public long clean(long ttlInMs) {
synchronized (StagingManager.class) {
log.trace("Cleaning staging area");
Set<String> keys = new HashSet<String>(resourceList.keySet());
long purgedFileCount = 0;
long purgedFileSize = 0;
long purgedMemCount = 0;
long purgedMemSize = 0;
for (String key : keys) {
IStagedResource resource = resourceList.get(key);
/* resource could have deleted itself between the time the keys were cloned and now */
if (resource != null) {
boolean resourceIsOld = (System.currentTimeMillis() - resource
.getLastUpdateTime()) > ttlInMs;
if ((resource.getState() == State.READY || resource.getState() == State.DONE)
&& (resourceIsOld || !resource.exists())) {
if (!resource.isInUse()) {
boolean file = resource.isFileResource();
long size = resource.getSize();
if (resource.delete()) {
if (file) {
purgedFileCount++;
purgedFileSize += size;
} else {
purgedMemCount++;
purgedMemSize += size;
}
resourceList.remove(key);
} else {
log.warn("Failed to delete the '{}' staging resource",
resource.getPath());
}
} else {
log.info(
"The '{}' staging resource qualified for being cleaned, but was in use. It will not be cleaned right now",
resource.getPath());
}
}
}
}
if (purgedFileCount > 0) {
if (purgedFileSize < 1000) {
log.debug("Purged {} staged files, freeing {} bytes of disk space",
purgedFileCount, (int) (purgedFileSize));
} else {
log.debug("Purged {} staged files, freeing {} kbytes of disk space",
purgedFileCount, (int) (purgedFileSize / 1000));
}
}
if (purgedMemCount > 0) {
if (purgedMemSize < 1000) {
log.debug("Purged {} staged memory buffers, freeing {} bytes of memory",
purgedMemCount, (int) (purgedMemSize));
} else {
log.debug("Purged {} staged memory buffers, freeing {} kbytes of memory",
purgedMemCount, (int) (purgedMemSize / 1000));
}
}
return purgedFileCount + purgedMemCount;
}
}
/**
* Create a handle that can be written to
*/
public IStagedResource create(long memoryThresholdInBytes, Object... path) {
String filePath = buildFilePath(path);
StagedResource resource = new StagedResource(memoryThresholdInBytes, directory, filePath,
this);
this.resourceList.put(filePath, resource);
return resource;
}
protected String buildFilePath(Object... path) {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < path.length; i++) {
Object part = path[i];
if (part instanceof Number) {
part = StringUtils.leftPad(part.toString(), 10, "0");
}
buffer.append(part);
if (i < path.length - 1) {
buffer.append("/");
}
}
return buffer.toString();
}
public IStagedResource find(String path) {
IStagedResource resource = resourceList.get(path);
if (resource != null) {
if (!resource.exists()
&& (resource.getState() == State.READY || resource.getState() == State.DONE)) {
resource.delete();
resource = null;
}
}
return resource;
}
public IStagedResource find(Object... path) {
return find(buildFilePath(path));
}
}