Skip to content

Commit

Permalink
JSON Encoding: use correct encoding for error when failed to route co…
Browse files Browse the repository at this point in the history
…mmand

Signed-off-by: Luke DiGiovanna <[email protected]>
  • Loading branch information
lukedigiovanna committed Jul 18, 2024
1 parent d18135c commit d00d811
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 203 deletions.
2 changes: 1 addition & 1 deletion src/groups/mqb/mqba/mqba_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ int Application::processCommand(const bslstl::StringRef& source,
}

// otherwise, this is an original call. utilize router if necessary
mqba::RouteCommandManager routeCommandManager(cmd, command);
mqba::RouteCommandManager routeCommandManager(cmd, commandWithOptions);

bool shouldSelfExecute = true;
bsl::string selfName;
Expand Down
16 changes: 7 additions & 9 deletions src/groups/mqb/mqba/mqba_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,26 +240,24 @@ class Application {
bool fromReroute = false);

private:
// HELPER FUNCTIONS FOR ADMIN API ROUTING

// Returns a pointer to the cluster instance that the given command needs
// to execute for.
mqbi::Cluster* getRelevantCluster(const mqbcmd::CommandChoice& command,
mqbcmd::InternalResult* cmdResult) const;

// Executes the logic of the given command and outputs the result in
// cmdResult
int executeCommand(const mqbcmd::Command& commandWithOptions,
const mqbcmd::CommandChoice& command,
mqbcmd::InternalResult* cmdResult);

void printCommandResponses(const mqbcmd::RouteResponseList& responseList,
const mqbcmd::EncodingFormat::Value format,
const mqbcmd::EncodingFormat::Value encoding,
bsl::ostream& os) const;

void printCommandResult(const mqbcmd::InternalResult& result,
mqbcmd::EncodingFormat::Value encoding,
bsl::ostream& os);

// Executes the logic of the given command and outputs the result in
// cmdResult
int executeCommand(const mqbcmd::Command& commandWithOptions,
const mqbcmd::CommandChoice& command,
mqbcmd::InternalResult* cmdResult);
};

} // close package namespace
Expand Down
33 changes: 27 additions & 6 deletions src/groups/mqb/mqba/mqba_commandrouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <mqba_commandrouter.h>

// MQB
#include <mqbcmd_jsonprinter.h>
#include <mqbcmd_parseutil.h>
#include <mqbi_cluster.h>
#include <mqbnet_cluster.h>
Expand All @@ -33,10 +34,12 @@ namespace {
const char k_LOG_CATEGORY[] = "MQBA.COMMANDROUTER";
} // close unnamed namespace

RouteCommandManager::RouteCommandManager(const bsl::string& commandString,
const mqbcmd::CommandChoice& command)
RouteCommandManager::RouteCommandManager(
const bsl::string& commandString,
const mqbcmd::Command& commandWithOptions)
: d_commandString(commandString)
, d_command(command)
, d_commandWithOptions(commandWithOptions)
, d_command(commandWithOptions.choice())
, d_routingMode(getCommandRoutingMode())
, d_latch(1)
{
Expand Down Expand Up @@ -218,9 +221,27 @@ void RouteCommandManager::onRouteCommandResponse(
}
else {
// Something went wrong, possibly timed out
routeResponse.response() =
"Error ocurred sending command to node " +
pair.first->hostName();
bsl::string errorMessage =
"Error ocurred routing command, possibly timeout";
// if we are using JSON encoding
if (d_commandWithOptions.encoding() ==
mqbcmd::EncodingFormat::JSON_COMPACT ||
d_commandWithOptions.encoding() ==
mqbcmd::EncodingFormat::JSON_PRETTY) {
mqbcmd::Result result;
result.makeError().message() = errorMessage;
// encode result
mwcu::MemOutStream os;
bool pretty = d_commandWithOptions.encoding() ==
mqbcmd::EncodingFormat::JSON_PRETTY
? true
: false;
mqbcmd::JsonPrinter::print(os, result, pretty);
routeResponse.response() = os.str();
}
else { // otherwise human print it
routeResponse.response() = errorMessage;
}
}
d_responses.responses().push_back(routeResponse);
}
Expand Down
5 changes: 3 additions & 2 deletions src/groups/mqb/mqba/mqba_commandrouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class RouteCommandManager {

private:
const bsl::string& d_commandString;
const mqbcmd::Command& d_commandWithOptions;
const mqbcmd::CommandChoice& d_command;

mqbcmd::RouteResponseList d_responses;
Expand All @@ -119,8 +120,8 @@ class RouteCommandManager {
public:
/// Sets up a command router with the given command string and parsed
/// command object. This will
RouteCommandManager(const bsl::string& commandString,
const mqbcmd::CommandChoice& command);
RouteCommandManager(const bsl::string& commandString,
const mqbcmd::Command& commandWithOptions);

/// Returns true if this command router is necessary to route the command
/// that it was set up with. If the command does not require routing, then
Expand Down
5 changes: 0 additions & 5 deletions src/groups/mqb/mqbblp/mqbblp_cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -867,11 +867,6 @@ inline const mqbnet::Cluster& Cluster::netCluster() const
return *(d_clusterData.membership().netCluster());
}

// inline const mqbc::ClusterState& Cluster::clusterState() const
// {
// return d_state;
// }

inline const mqbcfg::ClusterDefinition* Cluster::clusterConfig() const
{
return &d_clusterData.clusterConfig();
Expand Down
8 changes: 0 additions & 8 deletions src/groups/mqb/mqbblp/mqbblp_clusterproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,6 @@ class ClusterProxy : public mqbc::ClusterStateObserver,
/// Return a reference not offering modifiable access to the net cluster
/// used by this cluster.
const mqbnet::Cluster& netCluster() const BSLS_KEYWORD_OVERRIDE;

// Returns a reference to the cluster state describing this cluster
// const mqbc::ClusterState& clusterState() const BSLS_KEYWORD_OVERRIDE;
};

// ============================================================================
Expand Down Expand Up @@ -827,11 +824,6 @@ inline const mqbnet::Cluster& ClusterProxy::netCluster() const
return *(d_clusterData.membership().netCluster());
}

// inline const mqbc::ClusterState& ClusterProxy::clusterState() const
// {
// return d_state;
// }

} // close package namespace
} // close enterprise namespace

Expand Down
Loading

0 comments on commit d00d811

Please sign in to comment.