This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Alias #267
Open
satang500
wants to merge
16
commits into
master
Choose a base branch
from
alias
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Alias #267
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0295f7e
temp do not push
17230d9
init not push
11aedfa
do not push
fa367eb
initCommit-only query master reads alias cacheref T68833
87085e0
aliasCache2 init work
e23d236
alias cache init commit
7ac2e22
clean code
11ca37a
clean code
c7c3ddf
add cleaup in tests
cd152a9
added missing test
e9c40f6
remove unrelated file
e838491
clean up
d1be28d
clean up
8bf4250
modified code
fd25ffb
add licence to new files
a1931a9
removed unused variable
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,35 +13,83 @@ | |
*/ | ||
package com.addthis.hydra.job.alias; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
import com.addthis.hydra.job.store.DataStoreUtil; | ||
import com.addthis.hydra.job.store.SpawnDataStore; | ||
import com.addthis.hydra.query.spawndatastore.AliasBiMap; | ||
import com.addthis.hydra.query.spawndatastore.AliasCache; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Strings; | ||
import com.google.common.base.Throwables; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@ThreadSafe | ||
public class AliasManagerImpl implements AliasManager { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AliasManagerImpl.class); | ||
private Map<String, String> aliases; | ||
/* This SpawnDataStore must be the same type (zookeeper/priam) between Spawn and Mqmaster. This should | ||
* be guaranteed by the implementation of DataStoreUtil. */ | ||
public static final String ALIAS_PATH = "/query/alias"; | ||
private final SpawnDataStore spawnDataStore; | ||
private final Map<String, List<String>> alias2jobs; | ||
private final Map<String, String> job2alias; | ||
private final ObjectMapper mapper; | ||
private final ReentrantLock mapLock; | ||
private final AliasCache ac; | ||
|
||
private AliasBiMap aliasBiMap; | ||
public AliasManagerImpl() throws Exception{ | ||
this.spawnDataStore = DataStoreUtil.makeCanonicalSpawnDataStore(); | ||
this.mapLock = new ReentrantLock(); | ||
this.mapper = new ObjectMapper(); | ||
this.alias2jobs = new HashMap<>(); | ||
this.job2alias = new HashMap<>(); | ||
this.ac = new AliasCache(); | ||
} | ||
|
||
public AliasManagerImpl(SpawnDataStore spawnDataStore) { | ||
this.aliasBiMap = new AliasBiMap(spawnDataStore); | ||
aliasBiMap.loadCurrentValues(); | ||
public AliasManagerImpl(SpawnDataStore spawnDataStore) throws Exception { | ||
this.spawnDataStore = spawnDataStore; | ||
this.mapLock = new ReentrantLock(); | ||
this.mapper = new ObjectMapper(); | ||
this.alias2jobs = new HashMap<>(); | ||
this.job2alias = new HashMap<>(); | ||
this.ac = new AliasCache(); | ||
} | ||
|
||
/** | ||
* Returns a map describing alias name => jobIds | ||
*/ | ||
public Map<String, List<String>> getAliases() { | ||
return aliasBiMap.viewAliasMap(); | ||
aliases = spawnDataStore.getAllChildren(ALIAS_PATH); | ||
Map<String, List<String>> alias2Jobs = new HashMap<>(); | ||
for (Map.Entry<String, String> aliasEntry : aliases.entrySet()) { | ||
String key = aliasEntry.getKey(); | ||
List<String> jobs = decodeAliases(aliasEntry.getValue()); | ||
alias2Jobs.put(key, jobs); | ||
} | ||
return alias2Jobs; | ||
} | ||
|
||
public List<String> aliasToJobs(String alias) { | ||
return aliasBiMap.getJobs(alias); | ||
return getJobs(alias); | ||
} | ||
|
||
/** | ||
|
@@ -51,14 +99,161 @@ public List<String> aliasToJobs(String alias) { | |
*/ | ||
public void addAlias(String alias, List<String> jobs) { | ||
if (jobs.size() > 0) { | ||
aliasBiMap.putAlias(alias, jobs); | ||
putAlias(alias, jobs); | ||
} else { | ||
log.warn("Ignoring empty jobs addition for alias: {}", alias); | ||
} | ||
} | ||
|
||
public void deleteAlias(String alias) { | ||
aliasBiMap.deleteAlias(alias); | ||
public void putAlias(String alias, List<String> jobs) { | ||
mapLock.lock(); | ||
try { | ||
alias2jobs.put(alias, jobs); | ||
job2alias.put(jobs.get(0), alias); | ||
StringWriter sw = new StringWriter(); | ||
spawnDataStore.putAsChild(ALIAS_PATH, alias, mapper.writeValueAsString(jobs)); | ||
} catch (Exception e) { | ||
log.warn("failed to put alias: {}", alias, e); | ||
throw Throwables.propagate(e); | ||
} finally { | ||
mapLock.unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Get all jobIds for a given alias | ||
* | ||
* @param alias The alias to check | ||
* @return A list of jobIds, possible null | ||
*/ | ||
public List<String> getJobs(String alias) { | ||
refreshAlias(alias); | ||
mapLock.lock(); | ||
try { | ||
return alias2jobs.get(alias); | ||
} finally { | ||
mapLock.unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Refresh an alias based on datastore | ||
* | ||
* @param alias The alias to refresh | ||
*/ | ||
private void refreshAlias(String alias) { | ||
try { | ||
updateAlias(alias, this.spawnDataStore.getChild(ALIAS_PATH, alias)); | ||
} catch (ExecutionException e) { | ||
log.error("Failed to refresh alias: {}", alias, e); | ||
} catch (Exception e) { | ||
log.error("Unexpected error while refreshing alias {}", alias, e); | ||
} | ||
} | ||
|
||
/** | ||
* If jobs is available, update jobs in two maps based on the given alias | ||
* Otherwise, delete the job for a given alias from two maps | ||
* | ||
* @param alias The alias key to check | ||
* @param jobs The jobs to be updated for alias key | ||
* @return String The job that was updated | ||
*/ | ||
@Nullable private String updateAlias(String alias, @Nullable String jobs) throws Exception { | ||
if (Strings.isNullOrEmpty(alias)) { | ||
log.warn("Ignoring alias {} since it is null or empty ", alias); | ||
return jobs; | ||
} | ||
if (Strings.isNullOrEmpty(jobs)) { | ||
log.warn("Ignoring alias {} since there are no jobs and delete alias from two maps", alias); | ||
deleteAlias(alias); | ||
return jobs; | ||
} | ||
List<String> jobList = decodeAliases(jobs); | ||
mapLock.lock(); | ||
try { | ||
alias2jobs.put(alias, jobList); | ||
job2alias.put(jobList.get(0), alias); | ||
} finally { | ||
mapLock.unlock(); | ||
} | ||
return jobs; | ||
} | ||
|
||
@VisibleForTesting | ||
protected List<String> decodeAliases(@Nonnull String data) { | ||
try { | ||
return mapper.readValue(data, new TypeReference<List<String>>() {}); | ||
} catch (IOException e) { | ||
log.warn("Failed to decode data", e); | ||
return new ArrayList<>(0); | ||
} | ||
} | ||
|
||
/** | ||
* Delete the data for a given alias | ||
* | ||
* @param alias The alias to check | ||
*/ | ||
public void deleteAlias(String alias) throws Exception { | ||
spawnDataStore.deleteChild(ALIAS_PATH, alias); | ||
if( !Strings.isNullOrEmpty(spawnDataStore.getChild(ALIAS_PATH, alias))) { | ||
log.error("Fail to delete alias {} from spawn datastore", alias); | ||
return; | ||
} | ||
mapLock.lock(); | ||
try { | ||
List<String> jobs = alias2jobs.get(alias); | ||
alias2jobs.remove(alias); | ||
if ((jobs != null) && !jobs.isEmpty()) { | ||
for (String job : jobs) { | ||
String aliasVal = job2alias.get(job); | ||
if (Objects.equals(aliasVal, alias)) { | ||
job2alias.remove(job); | ||
} | ||
} | ||
} | ||
} finally { | ||
mapLock.unlock(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't you copy the maps first? What happens if the |
||
ac.deleteAlias(alias); | ||
} | ||
|
||
/** | ||
* Get an alias for a particular jobId | ||
* | ||
* @param jobid The jobId to check | ||
* @return One of the aliases for that job | ||
*/ | ||
public String getLikelyAlias(String jobid) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why likely alias? Wouldn't this be the alias? |
||
mapLock.lock(); | ||
try { | ||
String tmpAlias = job2alias.get(jobid); | ||
if (tmpAlias != null) { | ||
// Check to see if the alias has been deleted | ||
checkAlias(jobid, tmpAlias); | ||
} | ||
return job2alias.get(jobid); | ||
} finally { | ||
mapLock.unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Test a job/alias pair to see if an alias has disappeared | ||
* | ||
* @param job The job to test | ||
* @param alias The alias to check | ||
*/ | ||
private void checkAlias(String job, String alias) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method also has unclear side effects since it removes job from |
||
mapLock.lock(); | ||
try { | ||
if (!alias2jobs.containsKey(alias) && job2alias.get(job).equals(alias)) { | ||
job2alias.remove(job); | ||
} | ||
} finally { | ||
mapLock.unlock(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be declared statically.