split pick segment logic into its own method

This commit is contained in:
Nelson Ray 2013-01-28 15:06:09 -08:00
parent 94b72e8878
commit 6438401a32
3 changed files with 12 additions and 21 deletions

View File

@ -141,7 +141,7 @@ public class BalancerCostAnalyzer
} }
// The balancing application requires us to pick a proposal segment. // The balancing application requires us to pick a proposal segment.
public BalancerSegmentHolder findNewSegmentHome(List<ServerHolder> serverHolders, int numSegments) public BalancerSegmentHolder pickSegmentToMove(List<ServerHolder> serverHolders, int numSegments)
{ {
// We want to sample from each server w.p. numSegmentsOnServer / totalSegments // We want to sample from each server w.p. numSegmentsOnServer / totalSegments
ServerHolder fromServerHolder = sampleServer(serverHolders, numSegments); ServerHolder fromServerHolder = sampleServer(serverHolders, numSegments);
@ -151,9 +151,7 @@ public class BalancerCostAnalyzer
List<DataSegment> segments = Lists.newArrayList(fromServerHolder.getServer().getSegments().values()); List<DataSegment> segments = Lists.newArrayList(fromServerHolder.getServer().getSegments().values());
DataSegment proposalSegment = segments.get(rand.nextInt(segments.size())); DataSegment proposalSegment = segments.get(rand.nextInt(segments.size()));
ServerHolder toServer = findNewSegmentHome(proposalSegment, serverHolders); return new BalancerSegmentHolder(fromServerHolder.getServer(), proposalSegment);
return new BalancerSegmentHolder(fromServerHolder.getServer(), toServer.getServer(), proposalSegment);
} }
// The assignment application requires us to supply a proposal segment. // The assignment application requires us to supply a proposal segment.

View File

@ -27,19 +27,16 @@ import com.metamx.druid.client.DruidServer;
public class BalancerSegmentHolder public class BalancerSegmentHolder
{ {
private final DruidServer fromServer; private final DruidServer fromServer;
private final DruidServer toServer;
private final DataSegment segment; private final DataSegment segment;
private volatile int lifetime = 15; private volatile int lifetime = 15;
public BalancerSegmentHolder( public BalancerSegmentHolder(
DruidServer fromServer, DruidServer fromServer,
DruidServer toServer,
DataSegment segment DataSegment segment
) )
{ {
this.fromServer = fromServer; this.fromServer = fromServer;
this.toServer = toServer;
this.segment = segment; this.segment = segment;
} }
@ -48,11 +45,6 @@ public class BalancerSegmentHolder
return fromServer; return fromServer;
} }
public DruidServer getToServer()
{
return toServer;
}
public DataSegment getSegment() public DataSegment getSegment()
{ {
return segment; return segment;

View File

@ -120,10 +120,11 @@ public class DruidMasterBalancer implements DruidMasterHelper
int iter = 0; int iter = 0;
while (iter < maxSegmentsToMove) { while (iter < maxSegmentsToMove) {
BalancerSegmentHolder holder = analyzer.findNewSegmentHome(serverHolderList, numSegments); BalancerSegmentHolder segmentToMove = analyzer.pickSegmentToMove(serverHolderList, numSegments);
if (!segmentsBeingMoved.contains(holder.getSegment())) { DruidServer toServer = analyzer.findNewSegmentHome(segmentToMove.getSegment(), serverHolderList).getServer();
moveSegment(holder, params); if (!segmentsBeingMoved.contains(segmentToMove.getSegment())) {
segmentsBeingMoved.add(holder.getSegment()); moveSegment(segmentToMove, toServer, params);
segmentsBeingMoved.add(segmentToMove.getSegment());
} }
iter++; iter++;
} }
@ -160,14 +161,14 @@ public class DruidMasterBalancer implements DruidMasterHelper
private void moveSegment( private void moveSegment(
final BalancerSegmentHolder segment, final BalancerSegmentHolder segment,
final DruidServer toServer,
final DruidMasterRuntimeParams params final DruidMasterRuntimeParams params
) )
{ {
final DruidServer toServer = segment.getToServer(); final String toServerName = toServer.getName();
final String toServerName = segment.getToServer().getName();
final LoadQueuePeon toPeon = params.getLoadManagementPeons().get(toServerName); final LoadQueuePeon toPeon = params.getLoadManagementPeons().get(toServerName);
final String fromServer = segment.getFromServer().getName(); final String fromServerName = segment.getFromServer().getName();
final DataSegment segmentToMove = segment.getSegment(); final DataSegment segmentToMove = segment.getSegment();
final String segmentName = segmentToMove.getIdentifier(); final String segmentName = segmentToMove.getIdentifier();
@ -177,12 +178,12 @@ public class DruidMasterBalancer implements DruidMasterHelper
log.info( log.info(
"Moving [%s] from [%s] to [%s]", "Moving [%s] from [%s] to [%s]",
segmentName, segmentName,
fromServer, fromServerName,
toServerName toServerName
); );
try { try {
master.moveSegment( master.moveSegment(
fromServer, fromServerName,
toServerName, toServerName,
segmentToMove.getIdentifier(), segmentToMove.getIdentifier(),
new LoadPeonCallback() new LoadPeonCallback()