File: repl5_mtnode_ext.c

package info (click to toggle)
389-ds-base 2.3.1%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 37,536 kB
  • sloc: ansic: 306,972; python: 96,937; cpp: 10,257; perl: 2,854; makefile: 2,046; sh: 925; yacc: 806; xml: 379; lex: 366; javascript: 148; java: 50
file content (209 lines) | stat: -rw-r--r-- 6,224 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 *
 * License: GPL (version 3 or any later version).
 * See LICENSE for details.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


/* repl5_replica.c */

#include "repl5.h"
#include "cl5_api.h"

/* global data */
static DataList *root_list;

/*
 * Mapping tree node extension management. Node stores replica object
 */

void
multisupplier_mtnode_extension_init()
{
    /* Initialize list that store node roots. It is used during
       plugin startup to create replica objects */
    root_list = dl_new();
    dl_init(root_list, 0);
}

void
multisupplier_mtnode_extension_destroy()
{
    /* First iterate over the list to free the replica infos */
    dl_cleanup(root_list, (FREEFN)slapi_sdn_free);
    dl_free(&root_list);
}

/* This function loops over the list of node roots, constructing replica objects
   where exist */
void
multisupplier_mtnode_construct_replicas()
{
    Slapi_DN *root;
    int cookie;
    Replica *r;
    mapping_tree_node *mtnode;
    multisupplier_mtnode_extension *ext;

    for (root = dl_get_first(root_list, &cookie); root;
         root = dl_get_next(root_list, &cookie)) {
        r = replica_new(root);
        if (r) {

            mtnode = slapi_get_mapping_tree_node_by_dn(root);
            if (mtnode == NULL) {
                slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
                              "multisupplier_mtnode_construct_replicas: "
                              "failed to locate mapping tree node for %s\n",
                              slapi_sdn_get_dn(root));
                continue;
            }

            ext = (multisupplier_mtnode_extension *)repl_con_get_ext(REPL_CON_EXT_MTNODE, mtnode);
            if (ext == NULL) {
                slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "multisupplier_mtnode_construct_replicas: "
                                                                "failed to locate replication extension of mapping tree node for %s\n",
                              slapi_sdn_get_dn(root));
                continue;
            }

            ext->replica = object_new(r, replica_destroy);
            if (replica_add_by_name(replica_get_name(r), r) != 0) {
                if (ext->replica) {
                    object_release(ext->replica);
                    ext->replica = NULL;
                }
            }
            /* Wait a few seconds for everything to startup before resuming any replication tasks */
            slapi_eq_once_rel(replica_check_for_tasks, (void *)replica_get_root(r),
                              slapi_current_rel_time_t() + 5);
        }
    }
}

void *
multisupplier_mtnode_extension_constructor(void *object, void *parent __attribute__((unused)))
{
    mapping_tree_node *node;
    const Slapi_DN *root;
    multisupplier_mtnode_extension *ext;

    ext = (multisupplier_mtnode_extension *)slapi_ch_calloc(1, sizeof(multisupplier_mtnode_extension));

    node = (mapping_tree_node *)object;

    /* replica can be attached only to local public data */
    if (slapi_mapping_tree_node_is_set(node, SLAPI_MTN_LOCAL) &&
        !slapi_mapping_tree_node_is_set(node, SLAPI_MTN_PRIVATE)) {
        root = slapi_get_mapping_tree_node_root(node);
        /* ONREPL - we don't create replica object here because
           we can't fully initialize replica here since backends
           are not yet started. Instead, replica objects are created
           during replication plugin startup */
        if (root != NULL && !slapi_sdn_isempty(root)) {
            /* for now just store node root in the root list */
            dl_add(root_list, slapi_sdn_dup(root));
        }
    }

    return ext;
}

void
multisupplier_mtnode_extension_destructor(void *ext, void *object __attribute__((unused)), void *parent __attribute__((unused)))
{
    if (ext) {
        multisupplier_mtnode_extension *mtnode_ext = (multisupplier_mtnode_extension *)ext;
        if (mtnode_ext->replica) {
            object_release(mtnode_ext->replica);
            mtnode_ext->replica = NULL;
        }
        slapi_ch_free((void **)&ext);
    }
}

Replica *
replica_get_replica_from_dn(const Slapi_DN *dn)
{
    mapping_tree_node *mtnode;
    multisupplier_mtnode_extension *ext;
    Replica *r = NULL;

    if (dn == NULL)
        return NULL;

    mtnode = slapi_get_mapping_tree_node_by_dn(dn);
    if (mtnode == NULL) {
        slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
                                                        "failed to locate mapping tree node for %s\n",
                      slapi_sdn_get_dn(dn));
        return NULL;
    }

    ext = (multisupplier_mtnode_extension *)repl_con_get_ext(REPL_CON_EXT_MTNODE, mtnode);
    if (ext == NULL) {
        slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
                                                        "failed to locate replication extension of mapping tree node for %s\n",
                      slapi_sdn_get_dn(dn));
        return NULL;
    }

    if (ext->replica) {
        r = (Replica *)object_get_data(ext->replica);
    }

    return r;
}

Replica *
replica_get_replica_from_root(const char *repl_root)
{
    Replica *replica =NULL;
    Slapi_DN *repl_sdn = slapi_sdn_new_dn_byval(repl_root);

    replica = replica_get_replica_from_dn(repl_sdn);
    slapi_sdn_free(&repl_sdn);

    return replica;
}

Replica *
replica_get_replica_for_op(Slapi_PBlock *pb)
{
    Slapi_DN *sdn = NULL;
    Replica *replica = NULL;

    if (pb) {
        /* get replica generation for this operation */
        slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
        if (NULL == sdn) {
            goto bail;
        }
        replica = replica_get_replica_from_dn(sdn);
    }
bail:
    return replica;
}

Replica *
replica_get_for_backend(const char *be_name)
{
    Slapi_Backend *be;
    const Slapi_DN *suffix;

    be = slapi_be_select_by_instance_name(be_name);
    if (NULL == be)
        return NULL;

    suffix = slapi_be_getsuffix(be, 0);

    return replica_get_replica_from_dn(suffix);

}