Skip to content

Commit

Permalink
HPCC-30809 Revise based on review
Browse files Browse the repository at this point in the history
1. Remove 2 constructors.
2. Merge BackupWU into ArchiveWU with an optional 'deleteOnSuccess' option.
3. Store the sasha server response into the lastServerMessage and allow a caller to query it.
4. Store the ISashaCmdExecutors as members to avoid recreating.
5. Roll back the changes in esp_service_WsSMC.xsl.
6. More

Signed-off-by: wangkx <[email protected]>
  • Loading branch information
wangkx committed Dec 8, 2023
1 parent 9399d71 commit 0814710
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 155 deletions.
81 changes: 31 additions & 50 deletions dali/sasha/saruncmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,116 +4,97 @@
#include "sacmd.hpp"
#include "saruncmd.hpp"

class CSashaCmdExecutor : implements ISashaCmdExecutor, public CInterface
class CSashaCmdExecutor : public CInterfaceOf<ISashaCmdExecutor>
{
Linked<INode> node;
Owned<INode> node;
unsigned defaultTimeoutMs = 60;
StringBuffer nodeText;
const char *getNodeText()
{
if (0==nodeText.length())
node->endpoint().getEndpointHostText(nodeText);
return nodeText;
}
bool restoreWorkunit(const char *wuid, bool dfu, StringBuffer &serverResponse)
mutable StringBuffer lastServerMessage;
bool restoreWorkunit(const char *wuid, bool dfu) const
{
lastServerMessage.clear();
Owned<ISashaCommand> cmd = createSashaCommand();
cmd->setAction(SCA_RESTORE);
if (dfu)
cmd->setDFU(true);
cmd->addId(wuid);
if (!cmd->send(node, defaultTimeoutMs))
throw makeStringExceptionV(-1, "Could not connect to Sasha server on %s", getNodeText());
throw makeStringExceptionV(-1, "Could not connect to Sasha server on %s", nodeText.str());
if (cmd->numIds()==0)
{
// nothing restored
return false;
}
cmd->getId(0, serverResponse);
cmd->getId(0, lastServerMessage);
return true;
}
bool archiveWorkunit(const char *wuid, bool dfu, bool deleteAfterArchiving, StringBuffer &serverResponse)
bool archiveWorkunit(const char *wuid, bool dfu, bool deleteAfterArchiving) const
{
lastServerMessage.clear();
Owned<ISashaCommand> cmd = createSashaCommand();
cmd->setAction(SCA_ARCHIVE);
if (dfu)
cmd->setDFU(true);
cmd->addId(wuid);
if (!cmd->send(node, defaultTimeoutMs))
throw makeStringExceptionV(-1, "Could not connect to Sasha server on %s", getNodeText());
throw makeStringExceptionV(-1, "Could not connect to Sasha server on %s", nodeText.str());
if (cmd->numIds()==0)
{
// nothing archived
return false;
}
cmd->getId(0, serverResponse);
cmd->getId(0, lastServerMessage);
return true;
}

public:
IMPLEMENT_IINTERFACE;
CSashaCmdExecutor(INode *_node, unsigned _defaultTimeoutSecs=60) : node(_node)
{
defaultTimeoutMs = _defaultTimeoutSecs * 1000;
}
CSashaCmdExecutor(const SocketEndpoint &ep, unsigned _defaultTimeoutSecs = 60)
{
ep.getEndpointHostText(nodeText);
node.setown(createINode(ep));
defaultTimeoutMs = _defaultTimeoutSecs * 1000;
}
CSashaCmdExecutor(const char *server, unsigned port=0, unsigned _defaultTimeoutSecs = 60)
{
SocketEndpoint ep(server, port);
node.setown(createINode(ep));
defaultTimeoutMs = _defaultTimeoutSecs * 1000;
}
StringBuffer &getVersion(StringBuffer &version)
virtual StringBuffer &getVersion(StringBuffer &version) const override
{
Owned<ISashaCommand> cmd = createSashaCommand();
cmd->setAction(SCA_GETVERSION);
if (!cmd->send(node, defaultTimeoutMs))
throw makeStringExceptionV(-1, "Could not connect to Sasha server on %s", getNodeText());
throw makeStringExceptionV(-1, "Could not connect to Sasha server on %s", nodeText.str());
if (!cmd->getId(0, version))
throw makeStringExceptionV(-1, "Sasha server[%s]: Protocol error", getNodeText());
throw makeStringExceptionV(-1, "Sasha server[%s]: Protocol error", nodeText.str());
return version;
}
bool restoreECLWorkUnit(const char *wuid, StringBuffer &serverResponse)
virtual StringBuffer &getLastServerMessage() const override
{
return lastServerMessage;
}
virtual bool restoreECLWorkUnit(const char *wuid) const override
{
return restoreWorkunit(wuid, false, serverResponse);
return restoreWorkunit(wuid, false);
}
bool restoreDFUWorkUnit(const char *wuid, StringBuffer &serverResponse)
virtual bool restoreDFUWorkUnit(const char *wuid) const override
{
return restoreWorkunit(wuid, true, serverResponse);
return restoreWorkunit(wuid, true);
}
bool archiveECLWorkUnit(const char *wuid, StringBuffer &serverResponse)
virtual bool archiveECLWorkUnit(const char *wuid) const override
{
return archiveWorkunit(wuid, false, true, serverResponse);
return archiveWorkunit(wuid, false, true);
}
bool archiveDFUWorkUnit(const char *wuid, StringBuffer &serverResponse)
virtual bool archiveDFUWorkUnit(const char *wuid) const override
{
return archiveWorkunit(wuid, true, true, serverResponse);
return archiveWorkunit(wuid, true, true);
}
bool backupECLWorkUnit(const char *wuid, StringBuffer &serverResponse)
virtual bool backupECLWorkUnit(const char *wuid) const override
{
return archiveWorkunit(wuid, true, false, serverResponse);
return archiveWorkunit(wuid, false, false);
}
bool backupDFUWorkUnit(const char *wuid, StringBuffer &serverResponse)
virtual bool backupDFUWorkUnit(const char *wuid) const override
{
return archiveWorkunit(wuid, true, false, serverResponse);
return archiveWorkunit(wuid, true, false);
}
};

ISashaCmdExecutor *createSashaCmdExecutor(INode *node, unsigned defaultTimeoutSecs)
{
return new CSashaCmdExecutor(node, defaultTimeoutSecs);
}

ISashaCmdExecutor *createSashaCmdExecutor(const SocketEndpoint &ep, unsigned defaultTimeoutSecs)
{
return new CSashaCmdExecutor(ep, defaultTimeoutSecs);
}

ISashaCmdExecutor *createSashaCmdExecutor(const char *server, unsigned port, unsigned defaultTimeoutSecs)
{
return new CSashaCmdExecutor(server, port, defaultTimeoutSecs);
}
17 changes: 8 additions & 9 deletions dali/sasha/saruncmd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@

interface ISashaCmdExecutor : extends IInterface
{
virtual StringBuffer &getVersion(StringBuffer &version) = 0;
virtual bool restoreECLWorkUnit(const char *wuid, StringBuffer &serverResponse) = 0;
virtual bool restoreDFUWorkUnit(const char *wuid, StringBuffer &serverResponse) = 0;
virtual bool archiveECLWorkUnit(const char *wuid, StringBuffer &serverResponse) = 0;
virtual bool archiveDFUWorkUnit(const char *wuid, StringBuffer &serverResponse) = 0;
virtual bool backupECLWorkUnit(const char *wuid, StringBuffer &serverResponse) = 0;
virtual bool backupDFUWorkUnit(const char *wuid, StringBuffer &serverResponse) = 0;
virtual StringBuffer &getVersion(StringBuffer &version) const = 0;
virtual StringBuffer &getLastServerMessage() const = 0;
virtual bool restoreECLWorkUnit(const char *wuid) const = 0;
virtual bool restoreDFUWorkUnit(const char *wuid) const = 0;
virtual bool archiveECLWorkUnit(const char *wuid) const = 0;
virtual bool archiveDFUWorkUnit(const char *wuid) const = 0;
virtual bool backupECLWorkUnit(const char *wuid) const = 0;
virtual bool backupDFUWorkUnit(const char *wuid) const = 0;
};

extern SARUNCMD_API ISashaCmdExecutor *createSashaCmdExecutor(INode *node, unsigned defaultTimeoutSecs=60);
extern SARUNCMD_API ISashaCmdExecutor *createSashaCmdExecutor(const SocketEndpoint &ep, unsigned defaultTimeoutSecs=60);
extern SARUNCMD_API ISashaCmdExecutor *createSashaCmdExecutor(const char *server, unsigned port=0, unsigned defaultTimeoutSecs=60);

#endif
9 changes: 1 addition & 8 deletions esp/scm/ws_sasha.ecm
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,11 @@ ESPresponse [exceptions_inline, nil_remove] ResultResponse
string Result;
};

ESPrequest [nil_remove] BackupWURequest
{
string Wuid;
ESPenum WUTypes WUType;
};

ESPrequest [nil_remove] ArchiveWURequest
{
string Wuid;
ESPenum WUTypes WUType;
bool DeleteOnSuccess(true);
};

ESPrequest [nil_remove] RestoreWURequest
Expand All @@ -56,8 +51,6 @@ ESPservice [auth_feature("DEFERRED"), //This declares that the method logic hand
ESPmethod [auth_feature("SashaAccess:Access")] GetVersion(GetVersionRequest, ResultResponse);
ESPmethod [auth_feature("SashaAccess:FULL")] ArchiveWU(ArchiveWURequest, ResultResponse); //archive ECL WUs or DFU WUs
ESPmethod [auth_feature("SashaAccess:FULL")] RestoreWU(RestoreWURequest, ResultResponse); //restore ECL WUs or DFU WUs
//The ArchiveWU deletes the workunit from Dali whereas BackupWU leave it in place.
ESPmethod [auth_feature("SashaAccess:FULL")] BackupWU(BackupWURequest, ResultResponse);
};

SCMexportdef(WSSasha);
Expand Down
97 changes: 48 additions & 49 deletions esp/services/ws_sasha/ws_sashaservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ ISashaCmdExecutor* CWSSashaEx::createSashaCommandExecutor(const char* archiverTy
return createSashaCmdExecutor(ep);
}

ISashaCmdExecutor* CWSSashaEx::querySashaCommandExecutor(CWUTypes wuType)
{
CriticalBlock block(sect);
if (wuType == CWUTypes_ECL)
{
if (!eclWUSashaCommandExecutor)
eclWUSashaCommandExecutor.setown(createSashaCommandExecutor(wuArchiverType));
return eclWUSashaCommandExecutor.get();
}

if (!dfuWUSashaCommandExecutor)
dfuWUSashaCommandExecutor.setown(createSashaCommandExecutor(dfuwuArchiverType));
return dfuWUSashaCommandExecutor.get();
}

void CWSSashaEx::init(IPropertyTree* cfg, const char* process, const char* service)
{
}
Expand All @@ -43,8 +58,7 @@ bool CWSSashaEx::onGetVersion(IEspContext& context, IEspGetVersionRequest& req,
context.ensureFeatureAccess(FEATURE_URL, SecAccess_Read, ECLWATCH_SASHA_ACCESS_DENIED, "WSSashaEx.GetVersion: Permission denied.");

StringBuffer results;
Owned<ISashaCmdExecutor> executor = createSashaCommandExecutor(wuArchiverType);
executor->getVersion(results);
querySashaCommandExecutor(CWUTypes_ECL)->getVersion(results);
resp.setResult(results);
}
catch(IException* e)
Expand All @@ -55,39 +69,20 @@ bool CWSSashaEx::onGetVersion(IEspContext& context, IEspGetVersionRequest& req,
return true;
}

void CWSSashaEx::setResults(const char* wuid, const char* action, StringBuffer& results)
void CWSSashaEx::setResults(const char* lastServerMessage, const char* wuid, const char* action, IEspResultResponse& resp)
{
if (lastServerMessage)
{
resp.setResult(lastServerMessage);
return;
}

StringBuffer results;
if (isEmptyString(wuid))
results.appendf("No workunits %s.", action);
else
results.appendf("Workunit %s not %s", wuid, action);
}

bool CWSSashaEx::onBackupWU(IEspContext& context, IEspBackupWURequest& req, IEspResultResponse& resp)
{
try
{
context.ensureFeatureAccess(FEATURE_URL, SecAccess_Full, ECLWATCH_SASHA_ACCESS_DENIED, "WSSashaEx.BackupWU: Permission denied.");

const char* wuid = req.getWuid();
CWUTypes wuType = req.getWUType();
Owned<ISashaCmdExecutor> executor = createSashaCommandExecutor(wuType == CWUTypes_ECL ? wuArchiverType : dfuwuArchiverType);

StringBuffer results;
bool sashaCmdReturn = false;
if (wuType == CWUTypes_ECL)
sashaCmdReturn = executor->backupECLWorkUnit(isEmptyString(wuid) ? "*" : wuid, results);
else
sashaCmdReturn = executor->backupDFUWorkUnit(isEmptyString(wuid) ? "*" : wuid, results);
if (!sashaCmdReturn)
setResults(wuid, "backed up", results);
resp.setResult(results);
}
catch(IException* e)
{
FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
}
return true;
resp.setResult(results);
}

bool CWSSashaEx::onArchiveWU(IEspContext& context, IEspArchiveWURequest& req, IEspResultResponse& resp)
Expand All @@ -97,18 +92,25 @@ bool CWSSashaEx::onArchiveWU(IEspContext& context, IEspArchiveWURequest& req, IE
context.ensureFeatureAccess(FEATURE_URL, SecAccess_Full, ECLWATCH_SASHA_ACCESS_DENIED, "WSSashaEx.ArchiveWU: Permission denied.");

const char* wuid = req.getWuid();
CWUTypes wuType= req.getWUType();
Owned<ISashaCmdExecutor> executor = createSashaCommandExecutor(wuType == CWUTypes_ECL ? wuArchiverType : dfuwuArchiverType);
CWUTypes wuType = req.getWUType();

StringBuffer results;
bool sashaCmdReturn = false;
if (wuType == CWUTypes_ECL)
sashaCmdReturn = executor->archiveECLWorkUnit(isEmptyString(wuid) ? "*" : wuid, results);
ISashaCmdExecutor* executor = querySashaCommandExecutor(wuType);
bool sashaCmdSuccess = false;
if (req.getDeleteOnSuccess())
{
if ((wuType == CWUTypes_ECL))
sashaCmdSuccess = executor->archiveECLWorkUnit(isEmptyString(wuid) ? "*" : wuid);
else
sashaCmdSuccess = executor->archiveDFUWorkUnit(isEmptyString(wuid) ? "*" : wuid);
}
else
sashaCmdReturn = executor->archiveDFUWorkUnit(isEmptyString(wuid) ? "*" : wuid, results);
if (!sashaCmdReturn)
setResults(wuid, "archived", results);
resp.setResult(results);
{
if ((wuType == CWUTypes_ECL))
sashaCmdSuccess = executor->backupECLWorkUnit(isEmptyString(wuid) ? "*" : wuid);
else
sashaCmdSuccess = executor->backupDFUWorkUnit(isEmptyString(wuid) ? "*" : wuid);
}
setResults(sashaCmdSuccess ? executor->getLastServerMessage().str() : nullptr, wuid, "archived", resp);
}
catch(IException* e)
{
Expand All @@ -125,17 +127,14 @@ bool CWSSashaEx::onRestoreWU(IEspContext& context, IEspRestoreWURequest& req, IE

const char* wuid = req.getWuid();
CWUTypes wuType = req.getWUType();
Owned<ISashaCmdExecutor> executor = createSashaCommandExecutor(wuType == CWUTypes_ECL ? wuArchiverType : dfuwuArchiverType);

StringBuffer results;
bool sashaCmdReturn = false;
if (wuType == CWUTypes_ECL)
sashaCmdReturn = executor->restoreECLWorkUnit(isEmptyString(wuid) ? "*" : wuid, results);
ISashaCmdExecutor* executor = querySashaCommandExecutor(wuType);
bool sashaCmdSuccess = false;
if ((wuType == CWUTypes_ECL))
sashaCmdSuccess = executor->restoreECLWorkUnit(isEmptyString(wuid) ? "*" : wuid);
else
sashaCmdReturn = executor->restoreDFUWorkUnit(isEmptyString(wuid) ? "*" : wuid, results);
if (!sashaCmdReturn)
setResults(wuid, "restored", results);
resp.setResult(results);
sashaCmdSuccess = executor->restoreDFUWorkUnit(isEmptyString(wuid) ? "*" : wuid);
setResults(sashaCmdSuccess ? executor->getLastServerMessage().str() : nullptr, wuid, "restored", resp);
}
catch(IException* e)
{
Expand Down
9 changes: 5 additions & 4 deletions esp/services/ws_sasha/ws_sashaservice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@

class CWSSashaEx : public CWSSasha
{
CriticalSection sect;
Owned<ISashaCmdExecutor> eclWUSashaCommandExecutor, dfuWUSashaCommandExecutor;

ISashaCmdExecutor* createSashaCommandExecutor(const char* archiverType);
void setResults(const char* wuid, const char* action, StringBuffer& results);
ISashaCmdExecutor* querySashaCommandExecutor(CWUTypes wuType);
void setResults(const char* lastServerMessage, const char* wuid, const char* action, IEspResultResponse& resp);

public:
IMPLEMENT_IINTERFACE;

virtual void init(IPropertyTree* cfg, const char* process, const char* service) override;
virtual bool onGetVersion(IEspContext& context, IEspGetVersionRequest& req, IEspResultResponse& resp) override;
virtual bool onBackupWU(IEspContext& context, IEspBackupWURequest& req, IEspResultResponse& resp) override;
virtual bool onArchiveWU(IEspContext& context, IEspArchiveWURequest& req, IEspResultResponse& resp) override;
virtual bool onRestoreWU(IEspContext& context, IEspRestoreWURequest& req, IEspResultResponse& resp) override;
};
Expand Down
Loading

0 comments on commit 0814710

Please sign in to comment.