Blame SOURCES/0001-Issue-3729-cont-RFE-Extend-log-of-operations-statist.patch

4f630f
From 8b89bf22dea16956e4a21174f28ec11f32fc2db4 Mon Sep 17 00:00:00 2001
4f630f
From: tbordaz <tbordaz@redhat.com>
4f630f
Date: Mon, 21 Nov 2022 11:41:15 +0100
4f630f
Subject: [PATCH 1/3] Issue 3729 - (cont) RFE Extend log of operations
4f630f
 statistics in access log (#5538)
4f630f
4f630f
Bug description:
4f630f
	This is a continuation of the #3729
4f630f
	The previous fix did not manage internal SRCH, so
4f630f
	statistics of internal SRCH were not logged
4f630f
4f630f
Fix description:
4f630f
	For internal operation log_op_stat uses
4f630f
	connid/op_id/op_internal_id/op_nested_count that have been
4f630f
	computed log_result
4f630f
4f630f
	For direct operation log_op_stat uses info from the
4f630f
	operation itself (o_connid and o_opid)
4f630f
4f630f
	log_op_stat relies on operation_type rather than
4f630f
	o_tag that is not available for internal operation
4f630f
4f630f
relates: #3729
4f630f
4f630f
Reviewed by: Pierre Rogier
4f630f
---
4f630f
 .../tests/suites/ds_logs/ds_logs_test.py      | 90 ++++++++++++++++++-
4f630f
 ldap/servers/slapd/proto-slap.h               |  2 +-
4f630f
 ldap/servers/slapd/result.c                   | 74 +++++++++------
4f630f
 3 files changed, 136 insertions(+), 30 deletions(-)
4f630f
4f630f
diff --git a/dirsrvtests/tests/suites/ds_logs/ds_logs_test.py b/dirsrvtests/tests/suites/ds_logs/ds_logs_test.py
4f630f
index 865a6d0a3..67605438b 100644
4f630f
--- a/dirsrvtests/tests/suites/ds_logs/ds_logs_test.py
4f630f
+++ b/dirsrvtests/tests/suites/ds_logs/ds_logs_test.py
4f630f
@@ -13,7 +13,7 @@ import pytest
4f630f
 import subprocess
4f630f
 from lib389._mapped_object import DSLdapObject
4f630f
 from lib389.topologies import topology_st
4f630f
-from lib389.plugins import AutoMembershipPlugin, ReferentialIntegrityPlugin, AutoMembershipDefinitions
4f630f
+from lib389.plugins import AutoMembershipPlugin, ReferentialIntegrityPlugin, AutoMembershipDefinitions, MemberOfPlugin
4f630f
 from lib389.idm.user import UserAccounts
4f630f
 from lib389.idm.group import Groups
4f630f
 from lib389.idm.organizationalunit import OrganizationalUnits
4f630f
@@ -1254,6 +1254,94 @@ def test_stat_index(topology_st, request):
4f630f
 
4f630f
     request.addfinalizer(fin)
4f630f
 
4f630f
+def test_stat_internal_op(topology_st, request):
4f630f
+    """Check that statistics can also be collected for internal operations
4f630f
+
4f630f
+    :id: 19f393bd-5866-425a-af7a-4dade06d5c77
4f630f
+    :setup: Standalone Instance
4f630f
+    :steps:
4f630f
+        1. Check that nsslapd-statlog-level is 0 (default)
4f630f
+        2. Enable memberof plugins
4f630f
+        3. Create a user
4f630f
+        4. Remove access log (to only detect new records)
4f630f
+        5. Enable statistic logging nsslapd-statlog-level=1
4f630f
+        6. Check that on direct SRCH there is no 'Internal' Stat records
4f630f
+        7. Remove access log (to only detect new records)
4f630f
+        8. Add group with the user, so memberof triggers internal search
4f630f
+           and check it exists 'Internal' Stat records
4f630f
+    :expectedresults:
4f630f
+        1. Success
4f630f
+        2. Success
4f630f
+        3. Success
4f630f
+        4. Success
4f630f
+        5. Success
4f630f
+        6. Success
4f630f
+        7. Success
4f630f
+        8. Success
4f630f
+    """
4f630f
+
4f630f
+    inst = topology_st.standalone
4f630f
+
4f630f
+    # Step 1
4f630f
+    log.info("Assert nsslapd-statlog-level is by default 0")
4f630f
+    assert topology_st.standalone.config.get_attr_val_int("nsslapd-statlog-level") == 0
4f630f
+
4f630f
+    # Step 2
4f630f
+    memberof = MemberOfPlugin(inst)
4f630f
+    memberof.enable()
4f630f
+    inst.restart()
4f630f
+
4f630f
+    # Step 3 Add setup entries
4f630f
+    users = UserAccounts(inst, DEFAULT_SUFFIX, rdn=None)
4f630f
+    user = users.create(properties={'uid': 'test_1',
4f630f
+                                    'cn': 'test_1',
4f630f
+                                    'sn': 'test_1',
4f630f
+                                    'description': 'member',
4f630f
+                                    'uidNumber': '1000',
4f630f
+                                    'gidNumber': '2000',
4f630f
+                                    'homeDirectory': '/home/testuser'})
4f630f
+    # Step 4 reset accesslog
4f630f
+    topology_st.standalone.stop()
4f630f
+    lpath = topology_st.standalone.ds_access_log._get_log_path()
4f630f
+    os.unlink(lpath)
4f630f
+    topology_st.standalone.start()
4f630f
+
4f630f
+    # Step 5 enable statistics
4f630f
+    log.info("Set nsslapd-statlog-level: 1 to enable indexing statistics")
4f630f
+    topology_st.standalone.config.set("nsslapd-statlog-level", "1")
4f630f
+
4f630f
+    # Step 6 for direct SRCH only non internal STAT records
4f630f
+    entries = topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "uid=test_1")
4f630f
+    topology_st.standalone.stop()
4f630f
+    assert topology_st.standalone.ds_access_log.match('.*STAT read index.*')
4f630f
+    assert topology_st.standalone.ds_access_log.match('.*STAT read index: attribute.*')
4f630f
+    assert topology_st.standalone.ds_access_log.match('.*STAT read index: duration.*')
4f630f
+    assert not topology_st.standalone.ds_access_log.match('.*Internal.*STAT.*')
4f630f
+    topology_st.standalone.start()
4f630f
+
4f630f
+    # Step 7 reset accesslog
4f630f
+    topology_st.standalone.stop()
4f630f
+    lpath = topology_st.standalone.ds_access_log._get_log_path()
4f630f
+    os.unlink(lpath)
4f630f
+    topology_st.standalone.start()
4f630f
+
4f630f
+    # Step 8 trigger internal searches and check internal stat records
4f630f
+    groups = Groups(inst, DEFAULT_SUFFIX, rdn=None)
4f630f
+    group = groups.create(properties={'cn': 'mygroup',
4f630f
+                                      'member': 'uid=test_1,%s' % DEFAULT_SUFFIX,
4f630f
+                                      'description': 'group'})
4f630f
+    topology_st.standalone.restart()
4f630f
+    assert topology_st.standalone.ds_access_log.match('.*Internal.*STAT read index.*')
4f630f
+    assert topology_st.standalone.ds_access_log.match('.*Internal.*STAT read index: attribute.*')
4f630f
+    assert topology_st.standalone.ds_access_log.match('.*Internal.*STAT read index: duration.*')
4f630f
+
4f630f
+    def fin():
4f630f
+        log.info('Deleting user/group')
4f630f
+        user.delete()
4f630f
+        group.delete()
4f630f
+
4f630f
+    request.addfinalizer(fin)
4f630f
+
4f630f
 if __name__ == '__main__':
4f630f
     # Run isolated
4f630f
     # -s for DEBUG mode
4f630f
diff --git a/ldap/servers/slapd/proto-slap.h b/ldap/servers/slapd/proto-slap.h
4f630f
index 77832797b..c63ad8e74 100644
4f630f
--- a/ldap/servers/slapd/proto-slap.h
4f630f
+++ b/ldap/servers/slapd/proto-slap.h
4f630f
@@ -515,7 +515,7 @@ long long config_get_pw_minage(void);
4f630f
 long long config_get_pw_warning(void);
4f630f
 int config_get_errorlog_level(void);
4f630f
 int config_get_accesslog_level(void);
4f630f
-int config_get_statlog_level();
4f630f
+int config_get_statlog_level(void);
4f630f
 int config_get_securitylog_level(void);
4f630f
 int config_get_auditlog_logging_enabled(void);
4f630f
 int config_get_auditfaillog_logging_enabled(void);
4f630f
diff --git a/ldap/servers/slapd/result.c b/ldap/servers/slapd/result.c
4f630f
index c8b363cce..2ba205e04 100644
4f630f
--- a/ldap/servers/slapd/result.c
4f630f
+++ b/ldap/servers/slapd/result.c
4f630f
@@ -33,7 +33,7 @@ static long current_conn_count;
4f630f
 static PRLock *current_conn_count_mutex;
4f630f
 static int flush_ber(Slapi_PBlock *pb, Connection *conn, Operation *op, BerElement *ber, int type);
4f630f
 static char *notes2str(unsigned int notes, char *buf, size_t buflen);
4f630f
-static void log_op_stat(Slapi_PBlock *pb);
4f630f
+static void log_op_stat(Slapi_PBlock *pb, uint64_t connid, int32_t op_id, int32_t op_internal_id, int32_t op_nested_count);
4f630f
 static void log_result(Slapi_PBlock *pb, Operation *op, int err, ber_tag_t tag, int nentries);
4f630f
 static void log_entry(Operation *op, Slapi_Entry *e);
4f630f
 static void log_referral(Operation *op);
4f630f
@@ -2000,65 +2000,82 @@ notes2str(unsigned int notes, char *buf, size_t buflen)
4f630f
     return (buf);
4f630f
 }
4f630f
 
4f630f
+#define STAT_LOG_CONN_OP_FMT_INT_INT "conn=Internal(%" PRIu64 ") op=%d(%d)(%d)"
4f630f
+#define STAT_LOG_CONN_OP_FMT_EXT_INT "conn=%" PRIu64 " (Internal) op=%d(%d)(%d)"
4f630f
 static void
4f630f
-log_op_stat(Slapi_PBlock *pb)
4f630f
+log_op_stat(Slapi_PBlock *pb, uint64_t connid, int32_t op_id, int32_t op_internal_id, int32_t op_nested_count)
4f630f
 {
4f630f
-
4f630f
-    Connection *conn = NULL;
4f630f
     Operation *op = NULL;
4f630f
     Op_stat *op_stat;
4f630f
     struct timespec duration;
4f630f
     char stat_etime[ETIME_BUFSIZ] = {0};
4f630f
+    int internal_op;
4f630f
 
4f630f
     if (config_get_statlog_level() == 0) {
4f630f
         return;
4f630f
     }
4f630f
 
4f630f
-    slapi_pblock_get(pb, SLAPI_CONNECTION, &conn;;
4f630f
     slapi_pblock_get(pb, SLAPI_OPERATION, &op);
4f630f
+    internal_op = operation_is_flag_set(op, OP_FLAG_INTERNAL);
4f630f
     op_stat = op_stat_get_operation_extension(pb);
4f630f
 
4f630f
-    if (conn == NULL || op == NULL || op_stat == NULL) {
4f630f
+    if (op == NULL || op_stat == NULL) {
4f630f
         return;
4f630f
     }
4f630f
     /* process the operation */
4f630f
-    switch (op->o_tag) {
4f630f
-        case LDAP_REQ_BIND:
4f630f
-        case LDAP_REQ_UNBIND:
4f630f
-        case LDAP_REQ_ADD:
4f630f
-        case LDAP_REQ_DELETE:
4f630f
-        case LDAP_REQ_MODRDN:
4f630f
-        case LDAP_REQ_MODIFY:
4f630f
-        case LDAP_REQ_COMPARE:
4f630f
+    switch (operation_get_type(op)) {
4f630f
+        case SLAPI_OPERATION_BIND:
4f630f
+        case SLAPI_OPERATION_UNBIND:
4f630f
+        case SLAPI_OPERATION_ADD:
4f630f
+        case SLAPI_OPERATION_DELETE:
4f630f
+        case SLAPI_OPERATION_MODRDN:
4f630f
+        case SLAPI_OPERATION_MODIFY:
4f630f
+        case SLAPI_OPERATION_COMPARE:
4f630f
+        case SLAPI_OPERATION_EXTENDED:
4f630f
             break;
4f630f
-        case LDAP_REQ_SEARCH:
4f630f
+        case SLAPI_OPERATION_SEARCH:
4f630f
             if ((LDAP_STAT_READ_INDEX & config_get_statlog_level()) &&
4f630f
                 op_stat->search_stat) {
4f630f
                 struct component_keys_lookup *key_info;
4f630f
                 for (key_info = op_stat->search_stat->keys_lookup; key_info; key_info = key_info->next) {
4f630f
-                    slapi_log_stat(LDAP_STAT_READ_INDEX,
4f630f
-                                   "conn=%" PRIu64 " op=%d STAT read index: attribute=%s key(%s)=%s --> count %d\n",
4f630f
-                                   op->o_connid, op->o_opid,
4f630f
-                                   key_info->attribute_type, key_info->index_type, key_info->key,
4f630f
-                                   key_info->id_lookup_cnt);
4f630f
+                    if (internal_op) {
4f630f
+                        slapi_log_stat(LDAP_STAT_READ_INDEX,
4f630f
+                                       connid == 0 ? STAT_LOG_CONN_OP_FMT_INT_INT "STAT read index: attribute=%s key(%s)=%s --> count %d\n":
4f630f
+                                                     STAT_LOG_CONN_OP_FMT_EXT_INT "STAT read index: attribute=%s key(%s)=%s --> count %d\n",
4f630f
+                                       connid, op_id, op_internal_id, op_nested_count,
4f630f
+                                       key_info->attribute_type, key_info->index_type, key_info->key,
4f630f
+                                       key_info->id_lookup_cnt);
4f630f
+                    } else {
4f630f
+                        slapi_log_stat(LDAP_STAT_READ_INDEX,
4f630f
+                                       "conn=%" PRIu64 " op=%d STAT read index: attribute=%s key(%s)=%s --> count %d\n",
4f630f
+                                       connid, op_id,
4f630f
+                                       key_info->attribute_type, key_info->index_type, key_info->key,
4f630f
+                                       key_info->id_lookup_cnt);
4f630f
+                    }
4f630f
                 }
4f630f
                
4f630f
                 /* total elapsed time */
4f630f
                 slapi_timespec_diff(&op_stat->search_stat->keys_lookup_end, &op_stat->search_stat->keys_lookup_start, &duration);
4f630f
                 snprintf(stat_etime, ETIME_BUFSIZ, "%" PRId64 ".%.09" PRId64 "", (int64_t)duration.tv_sec, (int64_t)duration.tv_nsec);
4f630f
-                slapi_log_stat(LDAP_STAT_READ_INDEX,
4f630f
-                               "conn=%" PRIu64 " op=%d STAT read index: duration %s\n",
4f630f
-                               op->o_connid, op->o_opid, stat_etime); 
4f630f
+                if (internal_op) {
4f630f
+                    slapi_log_stat(LDAP_STAT_READ_INDEX,
4f630f
+                                   connid == 0 ? STAT_LOG_CONN_OP_FMT_INT_INT "STAT read index: duration %s\n":
4f630f
+                                                 STAT_LOG_CONN_OP_FMT_EXT_INT "STAT read index: duration %s\n",
4f630f
+                                   connid, op_id, op_internal_id, op_nested_count, stat_etime);
4f630f
+                } else {
4f630f
+                    slapi_log_stat(LDAP_STAT_READ_INDEX,
4f630f
+                                   "conn=%" PRIu64 " op=%d STAT read index: duration %s\n",
4f630f
+                                   op->o_connid, op->o_opid, stat_etime);
4f630f
+                }
4f630f
             }
4f630f
             break;
4f630f
-        case LDAP_REQ_ABANDON_30:
4f630f
-        case LDAP_REQ_ABANDON:
4f630f
+        case SLAPI_OPERATION_ABANDON:
4f630f
             break;
4f630f
 
4f630f
         default:
4f630f
             slapi_log_err(SLAPI_LOG_ERR,
4f630f
-                          "log_op_stat", "Ignoring unknown LDAP request (conn=%" PRIu64 ", tag=0x%lx)\n",
4f630f
-                          conn->c_connid, op->o_tag);
4f630f
+                          "log_op_stat", "Ignoring unknown LDAP request (conn=%" PRIu64 ", op_type=0x%lx)\n",
4f630f
+                          connid, operation_get_type(op));
4f630f
             break;
4f630f
     }
4f630f
 }
4f630f
@@ -2218,7 +2235,7 @@ log_result(Slapi_PBlock *pb, Operation *op, int err, ber_tag_t tag, int nentries
4f630f
             } else {
4f630f
                 ext_str = "";
4f630f
             }
4f630f
-            log_op_stat(pb);
4f630f
+            log_op_stat(pb, op->o_connid, op->o_opid, 0, 0);
4f630f
             slapi_log_access(LDAP_DEBUG_STATS,
4f630f
                              "conn=%" PRIu64 " op=%d RESULT err=%d"
4f630f
                              " tag=%" BERTAG_T " nentries=%d wtime=%s optime=%s etime=%s%s%s%s\n",
4f630f
@@ -2233,6 +2250,7 @@ log_result(Slapi_PBlock *pb, Operation *op, int err, ber_tag_t tag, int nentries
4f630f
             }
4f630f
         } else {
4f630f
             int optype;
4f630f
+            log_op_stat(pb, connid, op_id, op_internal_id, op_nested_count);
4f630f
 #define LOG_MSG_FMT " tag=%" BERTAG_T " nentries=%d wtime=%s optime=%s etime=%s%s%s\n"
4f630f
             slapi_log_access(LDAP_DEBUG_ARGS,
4f630f
                              connid == 0 ? LOG_CONN_OP_FMT_INT_INT LOG_MSG_FMT :
4f630f
-- 
4f630f
2.38.1
4f630f