Skip to content

Commit

Permalink
use static method to compare upstream info objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetr committed May 6, 2016
1 parent 485764c commit 42ac7b4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package com.hubspot.baragon.agent.managers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;

import javax.ws.rs.core.Response;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand All @@ -30,6 +21,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import javax.ws.rs.core.Response;

@Singleton
public class AgentRequestManager {
private static final Logger LOG = LoggerFactory.getLogger(AgentRequestManager.class);
Expand Down Expand Up @@ -164,13 +162,13 @@ private ServiceContext getApplyContext(BaragonRequest request) throws Exception
boolean present = false;
boolean toRemove = false;
for (UpstreamInfo currentUpstream : upstreams) {
if (currentUpstream.sameAs(existingUpstream)) {
if (UpstreamInfo.upstreamAndGroupMatches(currentUpstream, existingUpstream)) {
present = true;
break;
}
}
for (UpstreamInfo upstreamToRemove : request.getRemoveUpstreams()) {
if (upstreamToRemove.sameAs(existingUpstream)) {
if (UpstreamInfo.upstreamAndGroupMatches(upstreamToRemove, existingUpstream)) {
toRemove = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
public class UpstreamInfo {
public static final String DEFAULT_GROUP = "default";

public static boolean upstreamAndGroupMatches(UpstreamInfo a, UpstreamInfo b) {
return a.getUpstream().equals(b.getUpstream()) && a.getGroup().equals(b.getGroup());
}

private final String upstream;

@Size(max=250)
Expand Down Expand Up @@ -134,10 +138,6 @@ public boolean equals(Object o) {
return true;
}

public boolean sameAs(UpstreamInfo that) {
return upstream.equals(that.upstream) && group.equals(that.group);
}

@Override
public int hashCode() {
int result = upstream.hashCode();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
package com.hubspot.baragon.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.transaction.CuratorTransactionFinal;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Function;
Expand All @@ -31,6 +15,21 @@
import com.hubspot.baragon.models.BaragonServiceState;
import com.hubspot.baragon.models.UpstreamInfo;
import com.hubspot.baragon.utils.ZkParallelFetcher;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.transaction.CuratorTransactionFinal;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Singleton
public class BaragonStateDatastore extends AbstractDataStore {
Expand Down Expand Up @@ -152,7 +151,7 @@ public void updateService(BaragonRequest request) throws Exception {
private List<String> matchingUpstreamPaths(Collection<UpstreamInfo> currentUpstreams, UpstreamInfo toAdd) {
List<String> matchingPaths = new ArrayList<>();
for (UpstreamInfo upstreamInfo : currentUpstreams) {
if (upstreamInfo.sameAs(toAdd)) {
if (UpstreamInfo.upstreamAndGroupMatches(upstreamInfo, toAdd)) {
matchingPaths.add(upstreamInfo.getOriginalPath().or(upstreamInfo.getUpstream()));
}
}
Expand All @@ -161,7 +160,7 @@ private List<String> matchingUpstreamPaths(Collection<UpstreamInfo> currentUpstr

private String getRemovePath(Collection<UpstreamInfo> currentUpstreams, UpstreamInfo toRemove) {
for (UpstreamInfo upstreamInfo : currentUpstreams) {
if (upstreamInfo.sameAs(toRemove)) {
if (UpstreamInfo.upstreamAndGroupMatches(upstreamInfo, toRemove)) {
return upstreamInfo.getOriginalPath().or(upstreamInfo.toPath());
}
}
Expand Down

0 comments on commit 42ac7b4

Please sign in to comment.