Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADH-4927] Improve db exception handling #97

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*/
package org.smartdata.metaservice;

public class MetaServiceException extends Exception {
import java.io.IOException;

public class MetaServiceException extends IOException {

public MetaServiceException(String errorMsg) {
super(errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,15 @@
package org.smartdata.server.engine;

import org.smartdata.metastore.dao.CacheFileDao;
import org.smartdata.metastore.dao.Searchable;
import org.smartdata.metastore.model.SearchResult;
import org.smartdata.metastore.queries.PageRequest;
import org.smartdata.metastore.dao.SearchableService;
import org.smartdata.metastore.queries.sort.CachedFilesSortField;
import org.smartdata.model.CachedFileStatus;
import org.smartdata.model.request.CachedFileSearchRequest;

import java.util.List;

public class CachedFilesManager implements
Searchable<CachedFileSearchRequest, CachedFileStatus, CachedFilesSortField> {

private final CacheFileDao cacheFileDao;
public class CachedFilesManager extends
SearchableService<CachedFileSearchRequest, CachedFileStatus, CachedFilesSortField> {

public CachedFilesManager(CacheFileDao cacheFileDao) {
this.cacheFileDao = cacheFileDao;
}

@Override
public SearchResult<CachedFileStatus> search(
CachedFileSearchRequest searchRequest, PageRequest<CachedFilesSortField> pageRequest) {
return cacheFileDao.search(searchRequest, pageRequest);
}

@Override
public List<CachedFileStatus> search(CachedFileSearchRequest searchRequest) {
return cacheFileDao.search(searchRequest);
super(cacheFileDao, "cached files");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static org.smartdata.metastore.utils.MetaStoreUtils.logAndBuildMetastoreException;
import static org.smartdata.model.action.ScheduleResult.RETRY;
import static org.smartdata.model.action.ScheduleResult.isSuccessful;
import static org.smartdata.model.audit.UserActivityObject.CMDLET;
Expand Down Expand Up @@ -230,10 +231,8 @@ public void init() throws IOException {
loadCmdletsFromDb();
LOG.info("Initialized.");
} catch (MetaStoreException e) {
LOG.error("DB Connection error! Failed to get Max CmdletId/ActionId!", e);
throw new IOException(e);
} catch (IOException e) {
throw e;
throw logAndBuildMetastoreException(
LOG, "DB Connection error! Failed to get Max CmdletId!", e);
} catch (Exception t) {
throw new IOException(t);
}
Expand Down Expand Up @@ -368,7 +367,7 @@ private long submitCmdletInternal(String cmdlet) throws IOException {
LOG.debug("Received Cmdlet -> [ {} ]", cmdlet);
try {
if (StringUtils.isBlank(cmdlet)) {
throw new IOException("Cannot submit an empty action!");
throw new IllegalArgumentException("Cannot submit an empty action!");
}
CmdletDescriptor cmdletDescriptor = buildCmdletDescriptor(cmdlet);
return submitCmdlet(cmdletDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.smartdata.server.engine.rule.ExecutorScheduler;
import org.smartdata.server.engine.rule.FileCopy2S3Plugin;
import org.smartdata.server.engine.rule.RuleExecutor;
import org.smartdata.server.engine.rule.RuleInfoHandler;
import org.smartdata.server.engine.rule.RuleInfoRepo;
import org.smartdata.server.engine.rule.SmallFilePlugin;
import org.smartdata.server.engine.rule.copy.FileCopyDrPlugin;
Expand Down Expand Up @@ -90,6 +91,7 @@ public class RuleManager
private final AuditService auditService;
private final SmartPrincipalManager smartPrincipalManager;
private final RuleDao ruleDao;
private final RuleInfoHandler ruleInfoHandler;

private boolean isClosed = false;

Expand Down Expand Up @@ -120,6 +122,7 @@ public RuleManager(
this.smartPrincipalManager = smartPrincipalManager;
this.metaStore = context.getMetaStore();
this.ruleDao = metaStore.ruleDao();
this.ruleInfoHandler = new RuleInfoHandler(ruleDao);
this.pathChecker = new PathChecker(context.getConf());

FileCopyScheduleStrategy copyScheduleStrategy = FileCopyScheduleStrategy.of(
Expand Down Expand Up @@ -149,7 +152,7 @@ public long submitRule(String rule, RuleState initState) throws IOException {
if (initState != RuleState.ACTIVE
&& initState != RuleState.DISABLED
&& initState != RuleState.NEW) {
throw new IOException(
throw new IllegalArgumentException(
"Invalid initState = "
+ initState
+ ", it MUST be one of ["
Expand All @@ -174,11 +177,7 @@ public long submitRule(String rule, RuleState initState) throws IOException {

RulePluginManager.onAddingNewRule(ruleInfo, tr);

try {
metaStore.insertNewRule(ruleInfo);
} catch (MetaStoreException e) {
throw new IOException("RuleText = " + rule, e);
}
metaStore.insertNewRule(ruleInfo);

RuleInfoRepo infoRepo = new RuleInfoRepo(ruleInfo, metaStore, serverContext.getConf());
mapRules.put(ruleInfo.getId(), infoRepo);
Expand Down Expand Up @@ -377,12 +376,13 @@ public SmartPrincipalManager getPrincipalService() {

@Override
public SearchResult<RuleInfo> search(
RuleSearchRequest searchRequest, PageRequest<RuleSortField> pageRequest) {
return ruleDao.search(searchRequest, pageRequest);
RuleSearchRequest searchRequest,
PageRequest<RuleSortField> pageRequest) throws Exception {
return ruleInfoHandler.search(searchRequest, pageRequest);
}

@Override
public List<RuleInfo> search(RuleSearchRequest searchRequest) {
return ruleDao.search(searchRequest);
public List<RuleInfo> search(RuleSearchRequest searchRequest) throws Exception {
return ruleInfoHandler.search(searchRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import org.smartdata.hdfs.action.move.AbstractMoveFileAction;
import org.smartdata.metastore.MetaStore;
import org.smartdata.metastore.MetaStoreException;
import org.smartdata.metastore.dao.ActionDao;
import org.smartdata.metastore.dao.Searchable;
import org.smartdata.metastore.model.SearchResult;
import org.smartdata.metastore.queries.PageRequest;
import org.smartdata.metastore.dao.SearchableService;
import org.smartdata.metastore.queries.sort.ActionSortField;
import org.smartdata.model.ActionInfo;
import org.smartdata.model.CmdletDescriptor;
Expand All @@ -50,19 +47,20 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.smartdata.metastore.utils.MetaStoreUtils.logAndBuildMetastoreException;

public class ActionInfoHandler
implements Searchable<ActionSearchRequest, ActionInfo, ActionSortField> {
extends SearchableService<ActionSearchRequest, ActionInfo, ActionSortField> {
private static final Logger LOG = LoggerFactory.getLogger(ActionInfoHandler.class);

private final MetaStore metaStore;
private final ActionDao actionDao;
private AtomicLong maxActionId;

private final InMemoryRegistry inMemoryRegistry;

public ActionInfoHandler(CmdletManagerContext context) {
super(context.getMetaStore().actionDao(), "actions");
this.metaStore = context.getMetaStore();
this.actionDao = metaStore.actionDao();
this.inMemoryRegistry = context.getInMemoryRegistry();
}

Expand All @@ -73,8 +71,8 @@ public void init() throws IOException {

LOG.info("Initialized.");
} catch (MetaStoreException e) {
LOG.error("DB Connection error! Failed to get Max CmdletId/ActionId!", e);
throw new IOException(e);
throw logAndBuildMetastoreException(
LOG, "DB Connection error! Failed to get Max ActionId!", e);
} catch (Exception t) {
throw new IOException(t);
}
Expand Down Expand Up @@ -105,8 +103,8 @@ public ActionInfo getActionInfoOrNull(long actionId) throws IOException {
? metaStore.getActionById(actionId)
: actionInfo;
} catch (MetaStoreException e) {
LOG.error("ActionId -> [ {} ], get ActionInfo from DB error", actionId, e);
throw new IOException(e);
throw logAndBuildMetastoreException(
LOG, "ActionId -> [ " + actionId + " ], get ActionInfo from DB error", e);
}
}

Expand All @@ -127,17 +125,17 @@ public List<ActionInfo> listNewCreatedActions(int actionNum) throws IOException
actionInfos.putAll(inMemoryRegistry.getUnfinishedActions());
return new ArrayList<>(actionInfos.values());
} catch (MetaStoreException e) {
LOG.error("Get Finished Actions from DB error", e);
throw new IOException(e);
throw logAndBuildMetastoreException(
LOG, "Get Finished Actions from DB error", e);
}
}

public List<ActionInfo> getActions(List<Long> aids) throws IOException {
try {
return metaStore.getActions(aids);
} catch (MetaStoreException e) {
LOG.error("Get Actions by aid list [{}] from DB error", aids.toString());
throw new IOException(e);
throw logAndBuildMetastoreException(
LOG, "Get Actions by aid list from DB error: " + aids.toString(), e);
}
}

Expand All @@ -157,18 +155,6 @@ public List<ActionInfo> createActionInfos(
.collect(Collectors.toList());
}

@Override
public SearchResult<ActionInfo> search(
ActionSearchRequest searchRequest,
PageRequest<ActionSortField> pageRequest) {
return actionDao.search(searchRequest, pageRequest);
}

@Override
public List<ActionInfo> search(ActionSearchRequest searchRequest) {
return actionDao.search(searchRequest);
}

private void updateActionStatusInternal(ActionInfo actionInfo, ActionStatus status) {
if (actionInfo.isFinished()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,22 @@
*/
package org.smartdata.server.engine.audit;

import org.smartdata.metastore.dao.Searchable;
import org.smartdata.metastore.dao.SearchableService;
import org.smartdata.metastore.dao.UserActivityDao;
import org.smartdata.metastore.model.SearchResult;
import org.smartdata.metastore.queries.PageRequest;
import org.smartdata.metastore.queries.sort.AuditSortField;
import org.smartdata.model.audit.UserActivityEvent;
import org.smartdata.model.request.AuditSearchRequest;

import java.util.List;

public class AuditService implements
Searchable<AuditSearchRequest, UserActivityEvent, AuditSortField> {
public class AuditService extends
SearchableService<AuditSearchRequest, UserActivityEvent, AuditSortField> {
private final UserActivityDao userActivityDao;

public AuditService(UserActivityDao userActivityDao) {
super(userActivityDao, "audit events");
this.userActivityDao = userActivityDao;
}

public void logEvent(UserActivityEvent event) {
userActivityDao.insert(event);
}

@Override
public SearchResult<UserActivityEvent> search(
AuditSearchRequest searchRequest, PageRequest<AuditSortField> pageRequest) {
return userActivityDao.search(searchRequest, pageRequest);
}

@Override
public List<UserActivityEvent> search(AuditSearchRequest searchRequest) {
return userActivityDao.search(searchRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import org.smartdata.exception.NotFoundException;
import org.smartdata.metastore.MetaStore;
import org.smartdata.metastore.MetaStoreException;
import org.smartdata.metastore.dao.CmdletDao;
import org.smartdata.metastore.dao.Searchable;
import org.smartdata.metastore.model.SearchResult;
import org.smartdata.metastore.queries.PageRequest;
import org.smartdata.metastore.dao.SearchableService;
import org.smartdata.metastore.queries.sort.CmdletSortField;
import org.smartdata.model.ActionInfo;
import org.smartdata.model.CmdletDescriptor;
Expand All @@ -50,19 +47,20 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.smartdata.metastore.utils.MetaStoreUtils.logAndBuildMetastoreException;

public class CmdletInfoHandler
implements Searchable<CmdletSearchRequest, CmdletInfo, CmdletSortField> {
extends SearchableService<CmdletSearchRequest, CmdletInfo, CmdletSortField> {
private static final Logger LOG = LoggerFactory.getLogger(CmdletInfoHandler.class);

private final MetaStore metaStore;
private final CmdletDao cmdletDao;
private final ActionInfoHandler actionInfoHandler;
private final InMemoryRegistry inMemoryRegistry;
private AtomicLong maxCmdletId;

public CmdletInfoHandler(CmdletManagerContext context, ActionInfoHandler actionInfoHandler) {
super(context.getMetaStore().cmdletDao(), "cmdlets");
this.metaStore = context.getMetaStore();
this.cmdletDao = metaStore.cmdletDao();
this.inMemoryRegistry = context.getInMemoryRegistry();
this.actionInfoHandler = actionInfoHandler;
}
Expand All @@ -73,8 +71,8 @@ public void init() throws IOException {
maxCmdletId = new AtomicLong(metaStore.getMaxCmdletId());
LOG.info("Initialized.");
} catch (MetaStoreException e) {
LOG.error("DB Connection error! Failed to get Max CmdletId!", e);
throw new IOException(e);
throw logAndBuildMetastoreException(
LOG, "DB Connection error! Failed to get Max CmdletId!", e);
}
}

Expand Down Expand Up @@ -112,8 +110,8 @@ public CmdletInfo getCmdletInfo(long cid) throws IOException {
? metaStore.getCmdletById(cid)
: cmdletInfo;
} catch (MetaStoreException e) {
LOG.error("CmdletId -> [ {} ], get CmdletInfo from DB error", cid, e);
throw new IOException(e);
throw logAndBuildMetastoreException(
LOG, "CmdletId -> [ " + cid + " ], get CmdletInfo from DB error", e);
}
}

Expand Down Expand Up @@ -147,19 +145,6 @@ public CmdletInfo getUnfinishedCmdlet(long cmdletId) {
return inMemoryRegistry.getUnfinishedCmdlet(cmdletId);
}

public List<CmdletInfo> listCmdletsInfo(
long ruleId, CmdletState cmdletState) throws IOException {
CmdletSearchRequest searchRequest = CmdletSearchRequest.builder()
.ruleId(ruleId)
.state(cmdletState)
.build();

return searchWithCache(
searchRequest,
cmdletInfo -> cmdletInfo.getRuleId() == ruleId
&& cmdletInfo.getState().equals(cmdletState));
}

public List<CmdletInfo> listCmdletsInfo(long ruleId) throws IOException {
CmdletSearchRequest searchRequest = CmdletSearchRequest.builder()
.ruleId(ruleId)
Expand Down Expand Up @@ -189,8 +174,8 @@ public boolean deleteCmdlet(long cmdletId) throws IOException {
metaStore.deleteCmdletActions(cmdletId);
return cmdletDeleted;
} catch (MetaStoreException e) {
LOG.error("CmdletId -> [ {} ], delete from DB error", cmdletId, e);
throw new IOException(e);
throw logAndBuildMetastoreException(
LOG, "CmdletId -> [ " + cmdletId + " ], delete from DB error", e);
}
}

Expand Down Expand Up @@ -237,17 +222,6 @@ public CmdletInfo updateCmdletStatus(long cmdletId, CmdletStatus status) {
return inMemoryRegistry.updateCmdlet(cmdletId, cmdlet -> updateCmdletStatus(cmdlet, status));
}

@Override
public SearchResult<CmdletInfo> search(
CmdletSearchRequest searchRequest, PageRequest<CmdletSortField> pageRequest) {
return cmdletDao.search(searchRequest, pageRequest);
}

@Override
public List<CmdletInfo> search(CmdletSearchRequest searchRequest) {
return cmdletDao.search(searchRequest);
}

// todo after zeppelin removal check if we really need this
// strongly consistent version of search
private List<CmdletInfo> searchWithCache(
Expand All @@ -262,7 +236,7 @@ private List<CmdletInfo> searchWithCache(
Function.identity()
));
} catch (Exception exception) {
throw new IOException("Error loading cmdlets from db", exception);
throw new MetaStoreException("Error loading cmdlets from db", exception);
}

for (CmdletInfo info : inMemoryRegistry.getUnfinishedCmdlets().values()) {
Expand Down
Loading
Loading