forked from elastic/ml-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Make controller send responses for each command received
This change makes the controller process respond to each command it receives with a document indicating whether that command was successfully executed or not. This response will be used by the Java side of the connection to determine when it is appropriate to move on to the next phase of the action that the controller command was part of. For example, when starting a process and connecting named pipes to it it is best that the named pipe connections are not attempted until the process is confirmed to be started. Relates elastic/elasticsearch#62823
- Loading branch information
1 parent
53216fe
commit 0e6268c
Showing
11 changed files
with
315 additions
and
101 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
#include "CResponseJsonWriter.h" | ||
|
||
namespace ml { | ||
namespace controller { | ||
namespace { | ||
|
||
// JSON field names | ||
const std::string ID{"id"}; | ||
const std::string SUCCESS{"success"}; | ||
const std::string REASON{"reason"}; | ||
} | ||
|
||
CResponseJsonWriter::CResponseJsonWriter(std::ostream& responseStream) | ||
: m_WriteStream{responseStream}, m_Writer{m_WriteStream} { | ||
} | ||
|
||
void CResponseJsonWriter::writeResponse(std::uint32_t id, bool success, const std::string& reason) { | ||
m_Writer.StartObject(); | ||
m_Writer.Key(ID); | ||
m_Writer.Uint(id); | ||
m_Writer.Key(SUCCESS); | ||
m_Writer.Bool(success); | ||
m_Writer.Key(REASON); | ||
m_Writer.String(reason); | ||
m_Writer.EndObject(); | ||
m_Writer.Flush(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
#ifndef INCLUDED_ml_controller_CResponseJsonWriter_h | ||
#define INCLUDED_ml_controller_CResponseJsonWriter_h | ||
|
||
#include <core/CRapidJsonLineWriter.h> | ||
|
||
#include <rapidjson/ostreamwrapper.h> | ||
|
||
#include <iosfwd> | ||
#include <string> | ||
|
||
namespace ml { | ||
namespace controller { | ||
|
||
//! \brief | ||
//! Write a response to a controller command in JSON format. | ||
//! | ||
//! DESCRIPTION:\n | ||
//! Output documents are of the form: | ||
//! | ||
//! { "id" : 123, "success" : true, "reason" : "message explaining success/failure" } | ||
//! | ||
//! A newline is written after each document, i.e. the output is ND-JSON. | ||
//! | ||
//! IMPLEMENTATION DECISIONS:\n | ||
//! Not using the concurrent line writer, as there's no need for thread | ||
//! safety. | ||
//! | ||
class CResponseJsonWriter { | ||
public: | ||
//! \param[in] responseStream The stream to which to write responses. | ||
CResponseJsonWriter(std::ostream& responseStream); | ||
|
||
//! Writes a response in JSON format. | ||
void writeResponse(std::uint32_t id, bool success, const std::string& reason); | ||
|
||
private: | ||
//! JSON writer ostream wrapper | ||
rapidjson::OStreamWrapper m_WriteStream; | ||
|
||
using TGenericLineWriter = core::CRapidJsonLineWriter<rapidjson::OStreamWrapper>; | ||
|
||
//! JSON writer | ||
TGenericLineWriter m_Writer; | ||
}; | ||
} | ||
} | ||
|
||
#endif // INCLUDED_ml_controller_CResponseJsonWriter_h |
Oops, something went wrong.