Skip to content

Commit d3efb18

Browse files
committed
MGM: add new WebNotify implementation
1 parent 57c6a48 commit d3efb18

File tree

12 files changed

+901
-21
lines changed

12 files changed

+901
-21
lines changed

cmake/EosFindLibs.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ if(NOT PACKAGEONLY)
6868
find_package(libproc2)
6969
find_package(Scitokens REQUIRED)
7070
find_package(Protobuf3 REQUIRED)
71+
find_package(ActiveMQCPP REQUIRED)
7172

7273
if(NOT (PROCPS_FOUND OR LIBPROC2_FOUND))
7374
message(FATAL_ERROR "Could not find either procps 3.x or libproc2 (procps 4.x). "
@@ -174,4 +175,5 @@ else()
174175
add_library(JEMALLOC::JEMALLOC INTERFACE IMPORTED)
175176
add_library(EosGrpcGateway::EosGrpcGateway INTERFACE IMPORTED)
176177
add_library(fmt::fmt-header-only INTERFACE IMPORTED)
178+
add_library(ActiveMQCPP::ActiveMQCPP INTERFACE IMPORTED)
177179
endif()

cmake/FindActiveMQCPP.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# FindActiveMQCPP.cmake
2+
3+
# Locate the header
4+
find_path(ACTIVEMQCPP_INCLUDE_DIR
5+
NAMES cms/Connection.h
6+
PATH_SUFFIXES
7+
activemq-cpp
8+
activemq-cpp-3.9.5
9+
activemq-cpp-3.9
10+
activemq-cpp-3
11+
PATHS
12+
/usr/include
13+
/usr/local/include
14+
/opt/include
15+
)
16+
17+
# Locate the library
18+
find_library(ACTIVEMQCPP_LIBRARY
19+
NAMES activemq-cpp
20+
PATHS
21+
/usr/lib /usr/lib64
22+
/usr/local/lib /usr/local/lib64
23+
/opt/lib /opt/lib64
24+
)
25+
26+
include(FindPackageHandleStandardArgs)
27+
find_package_handle_standard_args(ActiveMQCPP
28+
REQUIRED_VARS ACTIVEMQCPP_LIBRARY ACTIVEMQCPP_INCLUDE_DIR
29+
)
30+
31+
if (ACTIVEMQCPP_FOUND AND NOT TARGET ActiveMQCPP::ActiveMQCPP)
32+
add_library(ActiveMQCPP::ActiveMQCPP UNKNOWN IMPORTED)
33+
34+
set_target_properties(ActiveMQCPP::ActiveMQCPP PROPERTIES
35+
IMPORTED_LOCATION "${ACTIVEMQCPP_LIBRARY}"
36+
INTERFACE_INCLUDE_DIRECTORIES "${ACTIVEMQCPP_INCLUDE_DIR}"
37+
)
38+
endif()

common/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# * along with this program. If not, see <http://www.gnu.org/licenses/>.*
2222
# ************************************************************************
2323

24-
include_directories(${CMAKE_SOURCE_DIR}/common/jwt-cpp/include/)
24+
include_directories(${CMAKE_SOURCE_DIR}/common/jwt-cpp/include/ ${CMAKE_BINARY_DIR})
2525

2626
#-------------------------------------------------------------------------------
2727
# CTA integration related operations
@@ -247,6 +247,7 @@ if (NOT CLIENT AND Linux)
247247
ShellExecutor.cc
248248
ShellCmd.cc
249249
FileSystem.cc
250+
WebNotify.cc
250251
http/HttpServer.cc
251252
http/HttpRequest.cc
252253
http/HttpResponse.cc
@@ -256,7 +257,10 @@ if (NOT CLIENT AND Linux)
256257
target_link_libraries(EosCommonServer-Objects PUBLIC
257258
qclient
258259
XROOTD::UTILS
259-
PROTOBUF::PROTOBUF)
260+
PROTOBUF::PROTOBUF
261+
ActiveMQCPP::ActiveMQCPP
262+
GRPC::grpc++
263+
)
260264

261265
target_compile_definitions(EosCommonServer-Objects PUBLIC
262266
-DSQLITE_NO_SYNC=1)
@@ -270,6 +274,7 @@ if (NOT CLIENT AND Linux)
270274
target_link_libraries(EosCommonServer PUBLIC
271275
EosCommonServer-Objects
272276
XrdSsiPbEosCta-Objects
277+
EosGrpcProto-Objects
273278
XrdMqClient)
274279

275280
target_compile_definitions(EosCommonServer PUBLIC

common/WebNotify.cc

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
//------------------------------------------------------------------------------
2+
// File: WebNotify.cc
3+
// Author: Andreas-Joachim Peters - CERN
4+
//------------------------------------------------------------------------------
5+
6+
/************************************************************************
7+
* EOS - the CERN Disk Storage System *
8+
* Copyright (C) 2025 CERN/Switzerland *
9+
* *
10+
* This program is free software: you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation, either version 3 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
* This program is distributed in the hope that it will be useful, *
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18+
* GNU General Public License for more details. *
19+
* *
20+
* You should have received a copy of the GNU General Public License *
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
22+
************************************************************************/
23+
24+
#include "common/Logging.hh"
25+
#include "common/Namespace.hh"
26+
#include "common/StringConversion.hh"
27+
#include "common/WebNotify.hh"
28+
#include <iostream>
29+
#include <memory>
30+
31+
// curl
32+
#include <curl/curl.h>
33+
#include <curl/easy.h>
34+
#include <json/json.h>
35+
36+
// active MQ
37+
#include <cms/Connection.h>
38+
#include <cms/ConnectionFactory.h>
39+
#include <cms/Session.h>
40+
#include <cms/TextMessage.h>
41+
#include <cms/MessageProducer.h>
42+
#include <decaf/lang/Thread.h>
43+
#include <decaf/util/UUID.h>
44+
#include <decaf/internal/util/concurrent/Threading.h>
45+
#include <activemq/library/ActiveMQCPP.h>
46+
47+
// grpc
48+
#ifdef EOS_GRPC
49+
#include <grpc++/grpc++.h>
50+
#include "proto/Rpc.grpc.pb.h"
51+
using eos::rpc::Eos;
52+
using eos::rpc::NotificationRequest;
53+
using eos::rpc::NotificationResponse;
54+
/*#include <grpc/grpc.h>
55+
#include <grpc/grpc_security.h>
56+
#include <grpcpp/grpcpp.h>
57+
#include <grpcpp/channel.h>
58+
#include <grpcpp/client_context.h>
59+
#include <grpcpp/create_channel.h>
60+
#include <grpcpp/support/channel_arguments.h>
61+
#include <grpc/impl/codegen/grpc_types.h>
62+
*/
63+
#endif
64+
65+
// QClient
66+
#include <qclient/QClient.hh>
67+
68+
using namespace cms;
69+
using namespace decaf::lang;
70+
using namespace std;
71+
72+
EOSCOMMONNAMESPACE_BEGIN;
73+
74+
75+
bool WebNotify::Notify(const std::string& protocol,
76+
const std::string& uri,
77+
const std::string& sport,
78+
const std::string& channel,
79+
const std::string& message,
80+
const std::string& stimeout)
81+
{
82+
WebNotify notify;
83+
try {
84+
int timeoutMs = stimeout.empty() ? 0 : std::stoi(stimeout);
85+
int port = sport.empty() ? 0 : std::stoi(sport);
86+
eos_static_debug("protocol='%s'", protocol.c_str());
87+
if (protocol == "http")
88+
return notify.sendHttpPostNotification(uri, message, timeoutMs);
89+
if (protocol == "grpc")
90+
return notify.sendGrpcNotification(uri, message, timeoutMs);
91+
if (protocol == "redis")
92+
return notify.sendQClientNotification(uri, port, channel, message, timeoutMs, true);
93+
if (protocol == "qclient")
94+
return notify.sendQClientNotification(uri, port, channel, message, timeoutMs, false);
95+
if (protocol == "amq")
96+
return notify.sendActiveMQNotification(uri, channel, message, timeoutMs);
97+
eos_static_err("msg=\"unsupported notification protocol specified\" protocol=\"%s\"", protocol.c_str());
98+
} catch (const std::exception& e) {
99+
eos_static_err("msg=\"invalid numeric input\" error=\"%s\"", e.what());
100+
}
101+
102+
return false;
103+
}
104+
105+
bool WebNotify::sendHttpPostNotification(const std::string& url, const std::string& message, long timeoutMs) {
106+
CURL* curl = curl_easy_init();
107+
if (!curl) return false;
108+
109+
struct curl_slist* headers = nullptr;
110+
headers = curl_slist_append(headers, "Content-Type: application/json");
111+
112+
std::string jsonPayload;
113+
if (!message.empty() && message.front() == '{') {
114+
jsonPayload = message;
115+
} else {
116+
jsonPayload = "{\"message\": \"" + message + "\"}";
117+
}
118+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
119+
curl_easy_setopt(curl, CURLOPT_POST, 1L);
120+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonPayload.c_str());
121+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
122+
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeoutMs);
123+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NoOpCallback);
124+
125+
CURLcode res = curl_easy_perform(curl);
126+
curl_slist_free_all(headers);
127+
curl_easy_cleanup(curl);
128+
129+
return (res == CURLE_OK);
130+
}
131+
132+
bool WebNotify::sendActiveMQNotification(const std::string& brokerURI, const std::string& queueName, const std::string& messageText, int timeoutMs) {
133+
static std::once_flag initFlag;
134+
135+
std::call_once(initFlag, [] {
136+
activemq::library::ActiveMQCPP::initializeLibrary();
137+
});
138+
139+
try {
140+
// Construct broker URI with connection timeout
141+
std::ostringstream fullBrokerURI;
142+
fullBrokerURI << brokerURI;
143+
if (brokerURI.find('?') == std::string::npos) {
144+
fullBrokerURI << "?";
145+
} else {
146+
fullBrokerURI << "&";
147+
}
148+
fullBrokerURI
149+
<< "connection.requestTimeout=" << timeoutMs
150+
<< "&wireFormat.maxInactivityDuration=" << timeoutMs
151+
<< "&wireFormat.maxInactivityDurationInitialDelay=" << timeoutMs
152+
<< "&transport.maxReconnectAttempts=0";
153+
// Create a ConnectionFactory
154+
std::unique_ptr<ConnectionFactory> connectionFactory(ConnectionFactory::createCMSConnectionFactory(fullBrokerURI.str()));
155+
156+
// Create a Connection
157+
std::unique_ptr<Connection> connection(connectionFactory->createConnection());
158+
connection->start();
159+
160+
// Create a Session
161+
std::unique_ptr<Session> session(connection->createSession(Session::AUTO_ACKNOWLEDGE));
162+
163+
// Create the Destination (queue)
164+
std::unique_ptr<Destination> destination(session->createQueue(queueName));
165+
166+
// Create a MessageProducer from the Session to the Topic or Queue
167+
std::unique_ptr<MessageProducer> producer(session->createProducer(destination.get()));
168+
producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT);
169+
170+
// Create the message and send it
171+
std::unique_ptr<TextMessage> message(session->createTextMessage(messageText));
172+
producer->send(message.get());
173+
return true;
174+
} catch (const CMSException& e) {
175+
std::cerr << "CMSException: ";
176+
e.printStackTrace();
177+
return false;
178+
} catch (const std::exception& e) {
179+
eos_static_err("exception='%s'", e.what());
180+
return false;
181+
} catch (...) {
182+
eos_static_err("Unknown exception occurred while sending ActiveMQ notification.");
183+
return false;
184+
}
185+
}
186+
187+
bool WebNotify::sendGrpcNotification(const std::string& target, const std::string& message, int timeoutMs)
188+
{
189+
#ifdef EOS_GRPC
190+
grpc::ChannelArguments ch_args;
191+
// This is the key: set connection timeout (in milliseconds)
192+
ch_args.SetInt("grpc.client_channel_backup_poll_interval_ms", timeoutMs);
193+
ch_args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, -1); // Unlimited if needed
194+
195+
auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(timeoutMs);
196+
std::shared_ptr<grpc::Channel> channel = grpc::CreateCustomChannel(
197+
target,
198+
grpc::InsecureChannelCredentials(),
199+
ch_args
200+
);
201+
202+
auto stub = eos::rpc::Eos::NewStub(channel);
203+
204+
NotificationRequest request;
205+
request.set_message(message);
206+
207+
grpc::ClientContext context;
208+
context.set_deadline(deadline);
209+
210+
NotificationResponse response;
211+
grpc::Status status = stub->Notify(&context, request, &response);
212+
if (status.ok()) {
213+
eos_static_debug("gRPC call succeeded");
214+
return response.success();
215+
} else {
216+
eos_static_err("msg=\"gRPC call failed\" errc=%d errmsg='%s'", status.error_code(), status.error_message().c_str());
217+
return false;
218+
}
219+
#else
220+
return false;
221+
#endif
222+
}
223+
224+
bool WebNotify::sendQClientNotification(const std::string& hostname, int port,
225+
const std::string& channel,
226+
const std::string& message,
227+
int timeoutMs,
228+
bool push) {
229+
using namespace qclient;
230+
231+
try {
232+
// Connect with socket timeout
233+
QClient client{hostname, port, {}};
234+
235+
// Send PUBLISH command
236+
std::string method = push ?"RPUSH":"PUBLISH";
237+
auto publish = client.exec(method, channel, message);
238+
qclient::redisReplyPtr reply = publish.get();
239+
if (reply && reply->type == REDIS_REPLY_INTEGER && reply->integer != 0) {
240+
eos_static_debug("msg=\"published\" subscribers=%d", reply->integer);
241+
return true;
242+
} else {
243+
eos_static_err("msg=\"unexpected or null reply from QuarkDB/REDIS")
244+
return false;
245+
}
246+
} catch (const std::exception& ex) {
247+
eos_static_err("msg=\"QuarkDB/REDIS connection or command error\" msg='%s'", ex.what());
248+
return false;
249+
}
250+
}
251+
252+
EOSCOMMONNAMESPACE_END;

0 commit comments

Comments
 (0)