make batch data segment announcer thread safe

This commit is contained in:
fjy 2013-08-12 17:53:06 -07:00
parent 1f4e0ea40c
commit 5ff216838d
1 changed files with 40 additions and 25 deletions

View File

@ -36,6 +36,7 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
/**
*/
@ -48,8 +49,10 @@ public class BatchDataSegmentAnnouncer extends AbstractDataSegmentAnnouncer
private final ObjectMapper jsonMapper;
private final String liveSegmentLocation;
private final Set<SegmentZNode> availableZNodes = Sets.newHashSet();
private final Map<DataSegment, SegmentZNode> segmentLookup = Maps.newHashMap();
private final Object lock = new Object();
private final Set<SegmentZNode> availableZNodes = new ConcurrentSkipListSet<SegmentZNode>();
private final Map<DataSegment, SegmentZNode> segmentLookup = Maps.newConcurrentMap();
public BatchDataSegmentAnnouncer(
DruidServerMetadata server,
@ -74,6 +77,7 @@ public class BatchDataSegmentAnnouncer extends AbstractDataSegmentAnnouncer
throw new ISE("byte size %,d exceeds %,d", newBytesLen, config.getMaxNumBytes());
}
synchronized (lock) {
// create new batch
if (availableZNodes.isEmpty()) {
SegmentZNode availableZNode = new SegmentZNode(makeServedSegmentPath(new DateTime().toString()));
@ -104,11 +108,16 @@ public class BatchDataSegmentAnnouncer extends AbstractDataSegmentAnnouncer
}
}
}
}
@Override
public void unannounceSegment(DataSegment segment) throws IOException
{
final SegmentZNode segmentZNode = segmentLookup.remove(segment);
if (segmentZNode == null) {
return;
}
segmentZNode.removeSegment(segment);
log.info("Unannouncing segment[%s] at path[%s]", segment.getIdentifier(), segmentZNode.getPath());
@ -170,7 +179,7 @@ public class BatchDataSegmentAnnouncer extends AbstractDataSegmentAnnouncer
return ZKPaths.makePath(liveSegmentLocation, zNode);
}
private class SegmentZNode
private class SegmentZNode implements Comparable<SegmentZNode>
{
private final String path;
@ -286,5 +295,11 @@ public class BatchDataSegmentAnnouncer extends AbstractDataSegmentAnnouncer
{
return path.hashCode();
}
@Override
public int compareTo(SegmentZNode segmentZNode)
{
return path.compareTo(segmentZNode.getPath());
}
}
}