summaryrefslogtreecommitdiffstats
path: root/trunk/etherpad/src/etherpad/pad/pad_migrations.js
blob: e81cf630065c9c09930d2869ab81cf5e7ffec6c5 (plain)
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
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed 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.
 */

import("sqlbase.sqlobj");
import("etherpad.pad.model");
import("etherpad.pad.easysync2migration");
import("sqlbase.sqlcommon");
import("sqlbase.sqlobj");
import("etherpad.log");
import("etherpad.pne.pne_utils");
jimport("java.util.concurrent.ConcurrentHashMap");
jimport("java.lang.System");
jimport("java.util.ArrayList");
jimport("java.util.Collections");

function onStartup() {
  if (! appjet.cache.pad_migrations) {
    appjet.cache.pad_migrations = {};
  }

  // this part can be removed when all pads are migrated on pad.spline.inf.fu-berlin.de
  //if (! pne_utils.isPNE()) {
  //  System.out.println("Building cache for live migrations...");
  //  initLiveMigration();
  //}
}

function initLiveMigration() {

  if (! appjet.cache.pad_migrations) {
    appjet.cache.pad_migrations = {};
  }
  appjet.cache.pad_migrations.doingAnyLiveMigrations = true;
  appjet.cache.pad_migrations.doingBackgroundLiveMigrations = true;
  appjet.cache.pad_migrations.padMap = new ConcurrentHashMap();

  // presence of a pad in padMap indicates migration is needed
  var padMap = _padMap();
  var migrationsNeeded = sqlobj.selectMulti("PAD_SQLMETA", {version: 1});
  migrationsNeeded.forEach(function(obj) {
    padMap.put(String(obj.id), {from: obj.version});
  });
}

function _padMap() {
  return appjet.cache.pad_migrations.padMap;
}

function _doingItLive() {
  return !! appjet.cache.pad_migrations.doingAnyLiveMigrations;
}

function checkPadStatus(padId) {
  if (! _doingItLive()) {
    return "ready";
  }
  var info = _padMap().get(padId);
  if (! info) {
    return "ready";
  }
  else if (info.migrating) {
    return "migrating";
  }
  else {
    return "oldversion";
  }
}

function ensureMigrated(padId, async) {
  if (! _doingItLive()) {
    return false;
  }

  var info = _padMap().get(padId);
  if (! info) {
    // pad is up-to-date
    return false;
  }
  else if (async && info.migrating) {
    // pad is already being migrated, don't wait on the lock
    return false;
  }

  return model.doWithPadLock(padId, function() {
    // inside pad lock...
    var info = _padMap().get(padId);
    if (!info) {
      return false;
    }
    // migrate from version 1 to version 2 in a transaction
    var migrateSucceeded = false;
    try {
      info.migrating = true;
      log.info("Migrating pad "+padId+" from version 1 to version 2...");

      var success = false;
      var whichTry = 1;
      while ((! success) && whichTry <= 3) {
        success = sqlcommon.inTransaction(function() {
          try {
            easysync2migration.migratePad(padId);
            sqlobj.update("PAD_SQLMETA", {id: padId}, {version: 2});
            return true;
          }
          catch (e if (e.toString().indexOf("try restarting transaction") >= 0)) {
            whichTry++;
            return false;
          }
        });
        if (! success) {
          java.lang.Thread.sleep(Math.floor(Math.random()*200));
        }
      }
      if (! success) {
        throw new Error("too many retries");
      }

      migrateSucceeded = true;
      log.info("Migrated pad "+padId+".");
      _padMap().remove(padId);
    }
    finally {
      info.migrating = false;
      if (! migrateSucceeded) {
	log.info("Migration failed for pad "+padId+".");
        throw new Error("Migration failed for pad "+padId+".");
      }
    }
    return true;
  });
}

function numUnmigratedPads() {
  if (! _doingItLive()) {
    return 0;
  }

  return _padMap().size();
}

////////// BACKGROUND MIGRATIONS

function _logPadMigration(runnerId, padNumber, padTotal, timeMs, fourCharResult, padId) {
  log.custom("pad_migrations", {
    runnerId: runnerId,
    padNumber: Math.round(padNumber+1),
    padTotal: Math.round(padTotal),
    timeMs: Math.round(timeMs),
    fourCharResult: fourCharResult,
    padId: padId});
}

function _getNeededMigrationsArrayList(filter) {
  var L = new ArrayList(_padMap().keySet());
  for(var i=L.size()-1; i>=0; i--) {
    if (! filter(String(L.get(i)))) {
      L.remove(i);
    }
  }
  return L;
}

function runBackgroundMigration(residue, modulus, runnerId) {
  var L = _getNeededMigrationsArrayList(function(padId) {
    return (padId.charCodeAt(0) % modulus) == residue;
  });
  Collections.shuffle(L);

  var totalPads = L.size();
  for(var i=0;i<totalPads;i++) {
    if (! appjet.cache.pad_migrations.doingBackgroundLiveMigrations) {
      break;
    }
    var padId = L.get(i);
    var result = "FAIL";
    var t1 = System.currentTimeMillis();
    try {
      if (ensureMigrated(padId, true)) {
        result = " OK "; // migrated successfully
      }
      else {
        result = " -- "; // no migration needed after all
      }
    }
    catch (e) {
      // e just says "migration failed", but presumably
      // inTransaction() printed a stack trace.
      // result == "FAIL", do nothing.
    }
    var t2 = System.currentTimeMillis();
    _logPadMigration(runnerId, i, totalPads, t2 - t1, result, padId);
  }
}