[GitHub] [qpid-dispatch] kgiusti commented on a change in pull request #1435: DISPATCH-2267 - IO-thread facility to watch for changes to address reachability

2021-11-16 Thread GitBox


kgiusti commented on a change in pull request #1435:
URL: https://github.com/apache/qpid-dispatch/pull/1435#discussion_r750481682



##
File path: src/router_core/address_watch.c
##
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "router_core_private.h"
+#include "qpid/dispatch/amqp.h"
+
+struct qdr_address_watch_t {
+DEQ_LINKS(struct qdr_address_watch_t);
+qdr_watch_handle_t  watch_handle;
+char   *address_hash;
+qdr_address_watch_update_t  handler;
+void   *context;
+};
+
+ALLOC_DECLARE(qdr_address_watch_t);
+ALLOC_DEFINE(qdr_address_watch_t);
+
+static void qdr_watch_invoker(qdr_core_t *core, qdr_general_work_t *work);
+static void qdr_core_watch_address_CT(qdr_core_t *core, qdr_action_t *action, 
bool discard);
+static void qdr_core_unwatch_address_CT(qdr_core_t *core, qdr_action_t 
*action, bool discard);
+static void qdr_address_watch_free_CT(qdr_address_watch_t *watch);
+
+//==
+// Core Interface Functions
+//==
+qdr_watch_handle_t qdr_core_watch_address(qdr_core_t *core,
+  const char *address,
+  characlass,
+  charphase,
+  qdr_address_watch_update_t  on_watch,
+  void   *context)
+{
+static sys_atomic_t next_handle;
+qdr_action_t *action = qdr_action(qdr_core_watch_address_CT, 
"watch_address");
+
+action->args.io.address   = qdr_field(address);
+action->args.io.address_class = aclass;
+action->args.io.address_phase = phase;
+action->args.io.watch_handler = on_watch;
+action->args.io.context   = context;
+action->args.io.value32_1 = sys_atomic_inc(_handle);
+
+qdr_action_enqueue(core, action);
+return action->args.io.value32_1;
+}
+
+
+void qdr_core_unwatch_address(qdr_core_t *core, qdr_watch_handle_t handle)
+{
+qdr_action_t *action = qdr_action(qdr_core_unwatch_address_CT, 
"unwatch_address");
+
+action->args.io.value32_1 = handle;
+qdr_action_enqueue(core, action);
+}
+
+
+//==
+// In-Core API Functions
+//==
+void qdr_trigger_address_watch_CT(qdr_core_t *core, qdr_address_t *addr)
+{
+const char  *address_hash = (char*) 
qd_hash_key_by_handle(addr->hash_handle);
+qdr_address_watch_t *watch= DEQ_HEAD(core->addr_watches);
+
+while (!!watch) {
+if (strcmp(watch->address_hash, address_hash) == 0) {
+qdr_general_work_t *work = qdr_general_work(qdr_watch_invoker);
+work->watch_handler = watch->handler;
+work->context   = watch->context;
+work->local_consumers   = DEQ_SIZE(addr->rlinks);
+work->in_proc_consumers = DEQ_SIZE(addr->subscriptions);
+work->remote_consumers  = qd_bitmask_cardinality(addr->rnodes);
+work->local_producers   = DEQ_SIZE(addr->inlinks);
+qdr_post_general_work_CT(core, work);
+}
+watch = DEQ_NEXT(watch);
+}
+}
+
+void qdr_address_watch_shutdown(qdr_core_t *core)
+{
+qdr_address_watch_t *watch = DEQ_HEAD(core->addr_watches);
+while (!!watch) {
+DEQ_REMOVE(core->addr_watches, watch);
+qdr_address_watch_free_CT(watch);
+watch = DEQ_HEAD(core->addr_watches);
+}
+}
+
+
+//==
+// Local Functions
+//==
+static void qdr_address_watch_free_CT(qdr_address_watch_t *watch)
+{
+free(watch->address_hash);
+free_qdr_address_watch_t(watch);
+}
+
+
+static void 

[GitHub] [qpid-dispatch] kgiusti commented on a change in pull request #1435: DISPATCH-2267 - IO-thread facility to watch for changes to address reachability

2021-11-16 Thread GitBox


kgiusti commented on a change in pull request #1435:
URL: https://github.com/apache/qpid-dispatch/pull/1435#discussion_r750472044



##
File path: include/qpid/dispatch/router_core.h
##
@@ -160,6 +169,64 @@ void qdr_send_to2(qdr_core_t *core, qd_message_t *msg, 
const char *addr,
   bool exclude_inprocess, bool control);
 
 
+/**
+ **
+ * Address watch functions
+ **
+ */
+
+typedef uint32_t qdr_watch_handle_t;
+
+/**
+ * Handler for updates on watched addresses.  This function shall be invoked 
on an IO thread.
+ * 
+ * Note:  This function will be invoked when a watched address has a change in 
reachability.
+ * It is possible that the function may be called when no change occurs, 
particularly when an
+ * address is removed from the core address table.
+ *
+ * @param context The opaque context supplied in the call to 
qdr_core_watch_address
+ * @param local_consumers Number of consuming (outgoing) links for this 
address on this router
+ * @param in_proc_consumers Number of in-process consumers for this address on 
this router
+ * @param remote_consumers Number of remote routers with consumers for this 
address
+ * @param local_producers Number of producing (incoming) links for this 
address on this router
+ */
+typedef void (*qdr_address_watch_update_t)(void *context,
+   uint32_t  local_consumers,
+   uint32_t  in_proc_consumers,
+   uint32_t  remote_consumers,
+   uint32_t  local_producers);
+
+/**
+ * qdr_core_watch_address
+ *
+ * Subscribe to watch for changes in the reachability for an address.  It is 
safe to invoke this
+ * function from an IO thread.
+ * 
+ * @param core Pointer to the core module
+ * @param address The address to be watched
+ * @param aclass Address class character
+ * @param phase Address phase character ('0' .. '9')
+ * @param on_watch The handler function
+ * @param context The opaque context sent to the handler on all invocations
+ * @return Watch handle to be used when canceling the watch
+ */
+qdr_watch_handle_t qdr_core_watch_address(qdr_core_t *core,
+  const char *address,
+  characlass,
+  charphase,
+  qdr_address_watch_update_t  on_watch,
+  void   *context);
+
+/**
+ * qdr_core_unwatch_address
+ * 
+ * Cancel an address watch subscription.  It is safe to invoke this function 
from an IO thread.

Review comment:
   It is possible for the update handler to be invoked during or after this 
call on another I/O thread. That brings up a possible race:  whatever the 
context pointer addresses has to remain valid until we're sure that the handler 
will no longer be invoked, which isn't possible to tell from the caller's point 
of view.
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@qpid.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



-
To unsubscribe, e-mail: dev-unsubscr...@qpid.apache.org
For additional commands, e-mail: dev-h...@qpid.apache.org