Project

General

Profile

1 581 berkley
/**
2
 *  '$RCSfile$'
3 2298 tao
 *    Purpose: A class to asyncronously force the replication of each server
4
 *             that has an entry in the xml_replication table.  When run,
5
 *             this thread communicates with each server in the list and
6
 *             solicites a read of an updated or newly inserted document
7 669 jones
 *             with a certain docid.
8 581 berkley
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10 669 jones
 *    Authors: Chad Berkley
11 581 berkley
 *
12 669 jones
 *   '$Author$'
13
 *     '$Date$'
14
 * '$Revision$'
15
 *
16
 * This program is free software; you can redistribute it and/or modify
17
 * it under the terms of the GNU General Public License as published by
18
 * the Free Software Foundation; either version 2 of the License, or
19
 * (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 581 berkley
 */
30 2298 tao
31 581 berkley
package edu.ucsb.nceas.metacat;
32
33
import java.util.*;
34
import java.io.*;
35
import java.sql.*;
36
import java.net.*;
37
import java.lang.*;
38
import java.text.*;
39
40 2663 sgarg
import org.apache.log4j.Logger;
41
42 669 jones
/**
43 2298 tao
 * A class to asyncronously force the replication of each server
44
 * that has an entry in the xml_replication table.  When run,
45
 * this thread communicates with each server in the list and
46
 * solicites a read of an updated or newly inserted document
47 669 jones
 * with a certain docid.
48
 */
49 581 berkley
public class ForceReplicationHandler implements Runnable
50
{
51
  private Thread btThread;
52
  private MetaCatUtil util = new MetaCatUtil();
53
  private String docid;
54
  private String action;
55 1022 tao
  private boolean xmlDocument;
56 581 berkley
  private boolean dbactionFlag = true;
57 2298 tao
  private ReplicationServerList serverLists = null;//Serverlist
58 1292 tao
  private int homeServerCode = 0; // home server code for the docid
59 2298 tao
  // When a metacat A got forcereplication
60 1292 tao
  // notification from B, then A will update its record. And A will notification
61
  // other metacat in its serverlist to update this docid if A is a hub. But we
62
  // don't want A to notify B again. B is nofitification of A
63
  private String notificationServer = null;
64 2663 sgarg
  private Logger logMetacat = Logger.getLogger(ForceReplicationHandler.class);
65 2298 tao
66
  //constant
67
  public static final String DELETE = "delete";
68
69
70
71 581 berkley
  /**
72 1292 tao
   * Constructor of ForceReplicationHandler
73 581 berkley
   * @param docid the docid to force replicate
74 2298 tao
   * @param the action that is being performed on the document (either
75 581 berkley
   *        INSERT or UPDATE)
76 1292 tao
   * @param xml the docid is a xml document or not (data file)
77 2298 tao
   * @param notificationServer, when a metacat A got forcereplication
78 1292 tao
   * notification from B, then A will update its record. And A will notification
79
   * other metacat in its serverlist to update this docid if A is a hub. But we
80
   * don't want A to notify B again. B is nofitification of A.
81 581 berkley
   */
82 2298 tao
  public ForceReplicationHandler(String docid, String action, boolean xml,
83 3230 tao
                                                   String myNotificationServer)
84 581 berkley
  {
85
    this.docid = docid;
86
    this.action = action;
87 1022 tao
    this.xmlDocument =xml;
88 1292 tao
    // Build a severLists from xml_replication table
89
    this.serverLists = new ReplicationServerList();
90
    // Get sever code for this docid
91
    try
92
    {
93
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
94
    }//try
95
    catch (Exception e)
96
    {
97 2663 sgarg
      logMetacat.error("Error in ForceReplicationHandle(): "
98
                                                          +e.getMessage());
99 1292 tao
    }//catch
100
    // Get the notification server
101
    this.notificationServer = myNotificationServer;
102 2298 tao
103 581 berkley
    if(this.action.equals(""))
104
    {
105
      dbactionFlag = false;
106
    }
107 2298 tao
108 581 berkley
    btThread = new Thread(this);
109
    btThread.setPriority(Thread.MIN_PRIORITY);
110
    btThread.start();
111
  }
112 2298 tao
113 581 berkley
  /**
114
   * Use this constructor when the action is implied.
115
   */
116 2298 tao
  public ForceReplicationHandler(String docid, boolean xml,
117 3230 tao
                                                String myNotificationServer )
118 581 berkley
  {
119
    this.docid = docid;
120 1022 tao
    this.xmlDocument = xml;
121 581 berkley
    dbactionFlag = false;
122 1292 tao
    // Build a severLists from xml_replication table
123
    this.serverLists = new ReplicationServerList();
124
    // Get home server code for this doicd
125
    try
126
    {
127
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
128
    }
129
     catch (Exception e)
130
    {
131 2663 sgarg
      logMetacat.error("Error in ForceReplicationHandle(): "
132
                                                          +e.getMessage());
133 1292 tao
    }//catch
134
    // Get notification server
135
    this.notificationServer = myNotificationServer;
136 581 berkley
    btThread = new Thread(this);
137
    btThread.setPriority(Thread.MIN_PRIORITY);
138
    btThread.start();
139
  }
140 2298 tao
141 1292 tao
  /**
142 2298 tao
   * Method to send force replication command to other server to get
143 1292 tao
   * a new or updated docid
144
   */
145 581 berkley
  public void run()
146
  {
147 2298 tao
148 2649 tao
149 1292 tao
      // URL for notifcation
150 1022 tao
      URL comeAndGetIt = null;
151 1030 tao
      // If no server in xml_replication table, metacat don't need do anything
152 1292 tao
      if (serverLists.isEmpty())
153 1030 tao
      {
154
        return;
155
      }
156 2298 tao
157
      // Thread seelping for some seconds to make sure the document was insert
158 1292 tao
      // into the database before we send force replication request
159
      int sleepTime = Integer.parseInt
160
                         (MetaCatUtil.getOption("forcereplicationwaitingtime"));
161 2649 tao
      try
162
      {
163
        Thread.sleep(sleepTime);
164
      }
165
      catch(Exception ee)
166
      {
167 2663 sgarg
        logMetacat.error("Couldn't sleep in force replication");
168 2649 tao
      }
169 2663 sgarg
      logMetacat.info("notification server:"+notificationServer);
170 1292 tao
      // Check every server in the serverlists
171
      for (int i=0; i<serverLists.size(); i++)
172 581 berkley
      {
173 1292 tao
        //Set comeAndGetIt null
174
        comeAndGetIt = null;
175 2649 tao
         // Get ReplicationServer object in index i
176 1292 tao
        ReplicationServer replicationServer = serverLists.serverAt(i);
177 2649 tao
        // Get this ReplicationServer 's name
178 1292 tao
        String server = replicationServer.getServerName();
179 2649 tao
        try
180
        {
181
182 2298 tao
183
        // If the server is the notification server, we don't notify it back
184 1292 tao
        // again, if server is null we don't replication it
185
        if (server!=null && !server.equals(notificationServer))
186 581 berkley
        {
187 2298 tao
188 1292 tao
          if(dbactionFlag)
189
          {
190
            // xml documents and server can replicate xml doucment
191
            if (xmlDocument && replicationServer.getReplication())
192 1022 tao
            {
193 1292 tao
              // If this docid's homeserver is localhost, replicate it
194
              if (homeServerCode==1)
195
              {
196 2298 tao
                MetacatReplication.replLog("force xml replicating to "
197 1292 tao
                                                                    + server);
198 2298 tao
                comeAndGetIt = new URL("https://" + server +
199
                                 "?action=forcereplicate&server=" +
200 2579 tao
                                  util.getLocalReplicationServerName()+
201 581 berkley
                                 "&docid=" + docid + "&dbaction=" +
202 3230 tao
                                  action);
203 2298 tao
                //over write the url for delete
204
                if (action != null && action.equals(DELETE))
205
                {
206
                  comeAndGetIt = new URL("https://" + server + "?action=" +
207
                                   MetacatReplication.FORCEREPLICATEDELETE +
208 3230 tao
                                  "&docid=" + docid +"&server="+ util.getLocalReplicationServerName());
209 2298 tao
210
                }
211 1292 tao
              }//if servercode==1
212
              else if (replicationServer.getHub()||
213
                   server.equals(MetacatReplication.
214
                                  getServerNameForServerCode(homeServerCode)))
215
              {
216
                // If the docid's home server is not local host, but local host
217
                // is a hub of this server, replicate the docid too.
218
                // If the server is homeserver of the docid, replication it too
219 2298 tao
                MetacatReplication.replLog("force xml replicating to "
220 1292 tao
                                                                    + server);
221 2298 tao
                comeAndGetIt = new URL("https://" + server +
222
                                 "?action=forcereplicate&server=" +
223 2579 tao
                                 util.getLocalReplicationServerName() +
224 1292 tao
                                 "&docid=" + docid + "&dbaction=" +
225 3230 tao
                                  action);
226 2298 tao
                //over write the url for delete
227
               if (action != null && action.equals(DELETE))
228
               {
229
                  comeAndGetIt = new URL("https://" + server + "?action=" +
230
                            MetacatReplication.FORCEREPLICATEDELETE +
231 3230 tao
                           "&docid=" + docid+"&server="+ util.getLocalReplicationServerName());
232 2298 tao
233
                }
234 1292 tao
              }//else
235 2298 tao
236 1292 tao
            }//if xmlDocument
237 1022 tao
            //It is data file and configured to handle data replication
238 1292 tao
            else if (replicationServer.getDataReplication())
239 1022 tao
            {
240 1292 tao
              // If the docid's home server is local host, replicate the docid
241
              if (homeServerCode==1)
242
              {
243 2298 tao
                MetacatReplication.replLog("force data replicating to "
244 1292 tao
                                                                      + server);
245 2298 tao
                comeAndGetIt = new URL("https://" + server +
246
                                 "?action=forcereplicatedatafile&server=" +
247 2579 tao
                                 util.getLocalReplicationServerName() +
248 1022 tao
                                 "&docid=" + docid + "&dbaction=" +
249 3230 tao
                                  action);
250 2298 tao
                //over write the url for delete
251
               if (action != null && action.equals(DELETE))
252
               {
253
                 comeAndGetIt = new URL("https://" + server + "?action=" +
254
                                 MetacatReplication.FORCEREPLICATEDELETE +
255 3230 tao
                                 "&docid=" + docid +"&server="+ util.getLocalReplicationServerName());
256 2298 tao
257
               }
258
259 1292 tao
              }//if serverCode==1
260
              else if (replicationServer.getHub()||
261
                server.equals(MetacatReplication.
262
                                    getServerNameForServerCode(homeServerCode)))
263
              {
264 2298 tao
                // If the docid's home server is not local host, but local host
265 1292 tao
                // is a hub of this server, replicate the docid too.
266
                // If the server is homeserver of the docid replication it too
267 2298 tao
                 MetacatReplication.replLog("force data replicating to "
268 1292 tao
                                                                      + server);
269 2298 tao
                comeAndGetIt = new URL("https://" + server +
270
                                 "?action=forcereplicatedatafile&server=" +
271 2579 tao
                                 util.getLocalReplicationServerName() +
272 1292 tao
                                 "&docid=" + docid + "&dbaction=" +
273 3230 tao
                                  action);
274 2298 tao
                //over write the url for delete
275
               if (action != null && action.equals(DELETE))
276
               {
277
                  comeAndGetIt = new URL("https://" + server + "?action=" +
278
                         MetacatReplication.FORCEREPLICATEDELETE +
279 3230 tao
                        "&docid=" + docid+"&server="+ util.getLocalReplicationServerName());
280 2298 tao
281
               }
282
283 1292 tao
              }//else if servercode==1
284
            }//else if data file
285 2298 tao
286 1292 tao
          }//if has explicite action
287
          else
288
          { // has implicite action
289
            MetacatReplication.replLog("force replicating (default action) to )"
290
                                                + server);
291
            // this docid is xml documents and can replicate xml doucment
292
            if (xmlDocument && replicationServer.getReplication())
293
            {
294
              // If homeserver of this doicd is local, replicate it
295
              if (homeServerCode ==1)
296 2298 tao
              {
297
                comeAndGetIt = new URL("https://" + server +
298
                                 "?action=forcereplicate&server=" +
299 2579 tao
                                 util.getLocalReplicationServerName()+
300 3230 tao
                                 "&docid=" + docid);
301 2298 tao
302 1292 tao
              }//if homeserver ==1
303
              else if (replicationServer.getHub()||
304
                   server.equals(MetacatReplication.
305
                   getServerNameForServerCode(homeServerCode)))
306
              {
307 2298 tao
                // If home server of this docid is not local host, but local
308 1292 tao
                // host is a hub for this server, replicate this doicd
309
                // If the server is homeserver of the docid, replication it too
310 2298 tao
                comeAndGetIt = new URL("https://" + server +
311
                                 "?action=forcereplicate&server=" +
312 2579 tao
                                 util.getLocalReplicationServerName() +
313 3230 tao
                                 "&docid=" + docid);
314 2298 tao
315 1292 tao
              }//else if
316 2298 tao
317 1292 tao
            }//if xmlDoucment
318 2298 tao
            else if (replicationServer.getDataReplication())
319 1292 tao
            { //It is datafile and server is configured to replicate data file
320 2298 tao
321 1292 tao
              //if home server is local host, replicate the data file
322
              if (homeServerCode ==1)
323
              {
324 2298 tao
                comeAndGetIt = new URL("https://" + server +
325
                                 "?action=forcereplicatedatafile&server=" +
326 2579 tao
                                 util.getLocalReplicationServerName() +
327 3230 tao
                                 "&docid=" + docid);
328 1292 tao
              }//if
329
              else if (replicationServer.getHub()||
330
                          server.equals(MetacatReplication.
331
                          getServerNameForServerCode(homeServerCode)))
332
              {
333
                // If home server is not local host, but the local host is a hub
334
                // For this server, replicate the data file
335
                // If the server is homeserver of the docid, replication it too
336 2298 tao
                comeAndGetIt = new URL("https://" + server +
337
                                 "?action=forcereplicatedatafile&server=" +
338 2579 tao
                                 util.getLocalReplicationServerName() +
339 3230 tao
                                 "&docid=" + docid);
340 2298 tao
341 1292 tao
              }//else
342
            }//else if
343
          }//else has implicit action
344 2298 tao
345 1292 tao
          //Make sure comeAndGetIt is not empty
346
          if ( comeAndGetIt != null && !comeAndGetIt.equals(""))
347
          {
348 2663 sgarg
            logMetacat.warn("sending message: "
349
                                                + comeAndGetIt.toString());
350 1292 tao
            String message = MetacatReplication.getURLContent(comeAndGetIt);
351
          }
352
          //send out the url.  message is a dummy variable as the target of
353
          //the URL never directly replies to the request.  this simply
354
          //invoces a read request from the server to this local machine.
355 2649 tao
         }//if notification server
356
        }
357
        catch (Exception ee)
358
        {
359 2663 sgarg
        	logMetacat.error("error in ForceReplicationHandler.run for server "
360
                    + server +" "+ee.getMessage());
361 2649 tao
             ee.printStackTrace();
362
        }
363 1292 tao
      }//for
364 2298 tao
365 2649 tao
366 2663 sgarg
      logMetacat.warn("exiting ForceReplicationHandler Thread");
367 1292 tao
  }//run
368
}//ForceReplication class