|
15 | 15 |
|
16 | 16 | package com.spectralogic.ds3client.helpers; |
17 | 17 |
|
18 | | -import com.google.common.collect.ImmutableMap; |
19 | 18 | import com.google.common.util.concurrent.Futures; |
20 | 19 | import com.google.common.util.concurrent.ListenableFuture; |
21 | 20 | import com.google.common.util.concurrent.ListeningExecutorService; |
22 | 21 | import com.google.common.util.concurrent.MoreExecutors; |
23 | 22 | import com.spectralogic.ds3client.Ds3Client; |
24 | 23 | import com.spectralogic.ds3client.models.BulkObject; |
25 | | -import com.spectralogic.ds3client.models.Objects; |
26 | | -import com.spectralogic.ds3client.models.JobNode; |
| 24 | +import com.spectralogic.ds3client.helpers.strategy.BlobStrategy; |
27 | 25 | import org.slf4j.Logger; |
28 | 26 | import org.slf4j.LoggerFactory; |
29 | 27 |
|
| 28 | +import java.io.Closeable; |
30 | 29 | import java.io.IOException; |
31 | | -import java.util.*; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
32 | 32 | import java.util.concurrent.Callable; |
33 | 33 | import java.util.concurrent.ExecutionException; |
34 | 34 | import java.util.concurrent.Executors; |
35 | 35 |
|
36 | | -class ChunkTransferrer { |
| 36 | +class ChunkTransferrer implements Closeable { |
37 | 37 | private final static Logger LOG = LoggerFactory.getLogger(ChunkTransferrer.class); |
| 38 | + |
38 | 39 | private final ItemTransferrer itemTransferrer; |
39 | | - private final Ds3Client mainClient; |
40 | 40 | private final JobPartTracker partTracker; |
41 | | - private final int maxParallelRequests; |
| 41 | + private final ListeningExecutorService executor; |
42 | 42 |
|
43 | 43 | public interface ItemTransferrer { |
44 | 44 | void transferItem(Ds3Client client, BulkObject ds3Object) throws IOException; |
45 | 45 | } |
46 | 46 |
|
47 | 47 | public ChunkTransferrer( |
48 | 48 | final ItemTransferrer transferrer, |
49 | | - final Ds3Client mainClient, |
50 | 49 | final JobPartTracker partTracker, |
51 | 50 | final int maxParallelRequests) { |
52 | 51 | this.itemTransferrer = transferrer; |
53 | | - this.mainClient = mainClient; |
54 | 52 | this.partTracker = partTracker; |
55 | | - this.maxParallelRequests = maxParallelRequests; |
56 | | - } |
57 | | - |
58 | | - public void transferChunks( |
59 | | - final Iterable<JobNode> nodes, |
60 | | - final Iterable<Objects> chunks) |
61 | | - throws IOException { |
62 | | - LOG.debug("Getting ready to process chunks"); |
63 | | - final ImmutableMap<UUID, JobNode> nodeMap = buildNodeMap(nodes); |
64 | 53 | LOG.debug("Starting executor service"); |
65 | | - final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(maxParallelRequests)); |
| 54 | + executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(maxParallelRequests)); |
66 | 55 | LOG.debug("Executor service started"); |
67 | | - try { |
68 | | - final List<ListenableFuture<?>> tasks = new ArrayList<>(); |
69 | | - for (final Objects chunk : chunks) { |
70 | | - LOG.debug("Processing parts for chunk: {}", chunk.getChunkId().toString()); |
71 | | - final Ds3Client client = getClient(nodeMap, chunk.getNodeId(), mainClient); |
72 | | - for (final BulkObject ds3Object : chunk.getObjects()) { |
73 | | - final ObjectPart part = new ObjectPart(ds3Object.getOffset(), ds3Object.getLength()); |
74 | | - if (this.partTracker.containsPart(ds3Object.getName(), part)) { |
75 | | - LOG.debug("Adding {} to executor for processing", ds3Object.getName()); |
76 | | - tasks.add(executor.submit(new Callable<Object>() { |
77 | | - @Override |
78 | | - public Object call() throws Exception { |
79 | | - LOG.debug("Processing {}", ds3Object.getName()); |
80 | | - ChunkTransferrer.this.itemTransferrer.transferItem(client, ds3Object); |
81 | | - ChunkTransferrer.this.partTracker.completePart(ds3Object.getName(), part); |
82 | | - return null; |
83 | | - } |
84 | | - })); |
85 | | - } |
86 | | - } |
87 | | - } |
88 | | - executeWithExceptionHandling(tasks); |
89 | | - } finally { |
90 | | - LOG.debug("Shutting down executor"); |
91 | | - executor.shutdown(); |
92 | | - } |
93 | 56 | } |
94 | 57 |
|
95 | | - private static Ds3Client getClient(final ImmutableMap<UUID, JobNode> nodeMap, final UUID nodeId, final Ds3Client mainClient) { |
96 | | - final JobNode jobNode = nodeMap.get(nodeId); |
| 58 | + public void transferChunks( |
| 59 | + final BlobStrategy blobStrategy) |
| 60 | + throws IOException, InterruptedException { |
| 61 | + final List<ListenableFuture<?>> tasks = new ArrayList<>(); |
| 62 | + |
| 63 | + final Iterable<JobPart> work = blobStrategy.getWork(); |
| 64 | + |
| 65 | + for (final JobPart jobPart : work) { |
| 66 | + final BulkObject blob = jobPart.getBulkObject(); |
| 67 | + final ObjectPart part = new ObjectPart(blob.getOffset(), blob.getLength()); |
97 | 68 |
|
98 | | - if (jobNode == null) { |
99 | | - LOG.warn("The jobNode was not found, returning the existing client"); |
100 | | - return mainClient; |
| 69 | + if (this.partTracker.containsPart(blob.getName(), part)) { |
| 70 | + LOG.debug("Adding {} offset {} to executor for processing", blob.getName(), blob.getOffset()); |
| 71 | + tasks.add(executor.submit(new Callable<Object>() { |
| 72 | + @Override |
| 73 | + public Object call() throws Exception { |
| 74 | + LOG.debug("Processing {} offset {}", blob.getName(), blob.getOffset()); |
| 75 | + ChunkTransferrer.this.itemTransferrer.transferItem(jobPart.getClient(), blob); |
| 76 | + blobStrategy.blobCompleted(blob); |
| 77 | + ChunkTransferrer.this.partTracker.completePart(blob.getName(), part); |
| 78 | + return null; |
| 79 | + } |
| 80 | + })); |
| 81 | + } |
101 | 82 | } |
102 | 83 |
|
103 | | - return mainClient.newForNode(jobNode); |
| 84 | + executeWithExceptionHandling(tasks); |
104 | 85 | } |
105 | 86 |
|
106 | | - private static ImmutableMap<UUID, JobNode> buildNodeMap(final Iterable<JobNode> nodes) { |
107 | | - final ImmutableMap.Builder<UUID, JobNode> nodeMap = ImmutableMap.builder(); |
108 | | - for (final JobNode node: nodes) { |
109 | | - nodeMap.put(node.getId(), node); |
110 | | - } |
111 | | - return nodeMap.build(); |
| 87 | + @Override |
| 88 | + public void close() throws IOException { |
| 89 | + LOG.debug("Shutting down executor"); |
| 90 | + executor.shutdown(); |
112 | 91 | } |
113 | 92 |
|
114 | 93 | private static void executeWithExceptionHandling(final List<ListenableFuture<?>> tasks) |
|
0 commit comments