use of com.ichi2.anki.exception.NoEnoughServerSpaceException in project AnkiChinaAndroid by ankichinateam.
the class MediaSyncer method sync.
public String sync(long restSpace) throws UnknownHttpResponseException, MediaSyncException, NoEnoughServerSpaceException {
// of this class about this difference to the original.
if (mCol.getMedia().needScan()) {
mCon.publishProgress(R.string.sync_media_find);
mCol.log("findChanges");
try {
mCol.getMedia().findChanges();
} catch (SQLException ignored) {
return "corruptMediaDB";
}
}
// begin session and check if in sync
int lastUsn = mCol.getMedia().lastUsn();
JSONObject ret = mServer.begin();
int srvUsn = ret.getInt("usn");
if ((lastUsn == srvUsn) && !(mCol.getMedia().haveDirty())) {
return "noChanges";
}
// loop through and process changes from server
mCol.log("last local usn is " + lastUsn);
mDownloadCount = 0;
while (true) {
// Allow cancellation (note: media sync has no finish command, so just throw)
if (Connection.getIsCancelled()) {
Timber.i("Sync was cancelled");
throw new RuntimeException("UserAbortedSync");
}
JSONArray data = mServer.mediaChanges(lastUsn);
mCol.log("mediaChanges resp count: ", data.length());
if (data.length() == 0) {
break;
}
List<String> need = new ArrayList<>();
lastUsn = data.getJSONArray(data.length() - 1).getInt(1);
for (int i = 0; i < data.length(); i++) {
// Allow cancellation (note: media sync has no finish command, so just throw)
if (Connection.getIsCancelled()) {
Timber.i("Sync was cancelled");
throw new RuntimeException("UserAbortedSync");
}
String fname = data.getJSONArray(i).getString(0);
int rusn = data.getJSONArray(i).getInt(1);
String rsum = null;
if (!data.getJSONArray(i).isNull(2)) {
// If `rsum` is a JSON `null` value, `.optString(2)` will
// return `"null"` as a string
rsum = data.getJSONArray(i).optString(2);
}
Pair<String, Integer> info = mCol.getMedia().syncInfo(fname);
String lsum = info.first;
int ldirty = info.second;
mCol.log(String.format(Locale.US, "check: lsum=%s rsum=%s ldirty=%d rusn=%d fname=%s", TextUtils.isEmpty(lsum) ? "" : lsum.subSequence(0, 4), TextUtils.isEmpty(rsum) ? "" : rsum.subSequence(0, 4), ldirty, rusn, fname));
if (!TextUtils.isEmpty(rsum)) {
// added/changed remotely
if (TextUtils.isEmpty(lsum) || !lsum.equals(rsum)) {
mCol.log("will fetch");
need.add(fname);
} else {
mCol.log("have same already");
}
mCol.getMedia().markClean(Collections.singletonList(fname));
} else if (!TextUtils.isEmpty(lsum)) {
// deleted remotely
if (ldirty == 0) {
mCol.log("delete local");
mCol.getMedia().syncDelete(fname);
} else {
// conflict: local add overrides remote delete
mCol.log("conflict; will send");
}
} else {
// deleted both sides
mCol.log("both sides deleted");
mCol.getMedia().markClean(Collections.singletonList(fname));
}
}
_downloadFiles(need);
mCol.log("update last usn to " + lastUsn);
// commits
mCol.getMedia().setLastUsn(lastUsn);
}
// at this point, we're all up to date with the server's changes,
// and we need to send our own
boolean updateConflict = false;
int toSend = mCol.getMedia().dirtyCount();
long needSize = mCol.getMedia().getMediaSizeNeededUpload();
if (needSize >= 0 && restSpace < needSize && Consts.loginAnkiChina()) {
Timber.d("No Enough Server Space Exception,rest is:" + restSpace + ",need size:" + needSize);
throw new NoEnoughServerSpaceException(restSpace, needSize);
}
while (true) {
Pair<File, List<String>> changesZip = mCol.getMedia().mediaChangesZip();
File zip = changesZip.first;
try {
List<String> fnames = changesZip.second;
if (fnames.size() == 0) {
break;
}
mCon.publishProgress(String.format(AnkiDroidApp.getAppResources().getString(R.string.sync_media_changes_count), toSend));
JSONArray changes = mServer.uploadChanges(zip);
int processedCnt = changes.getInt(0);
int serverLastUsn = changes.getInt(1);
mCol.getMedia().markClean(fnames.subList(0, processedCnt));
mCol.log(String.format(Locale.US, "processed %d, serverUsn %d, clientUsn %d", processedCnt, serverLastUsn, lastUsn));
if (serverLastUsn - processedCnt == lastUsn) {
mCol.log("lastUsn in sync, updating local");
lastUsn = serverLastUsn;
// commits
mCol.getMedia().setLastUsn(serverLastUsn);
} else {
mCol.log("concurrent update, skipping usn update");
// commit for markClean
mCol.getMedia().getDb().commit();
updateConflict = true;
}
toSend -= processedCnt;
} finally {
zip.delete();
}
}
if (updateConflict) {
mCol.log("restart sync due to concurrent update");
return sync(restSpace);
}
int lcnt = mCol.getMedia().mediacount();
String sRet = mServer.mediaSanity(lcnt);
if ("OK".equals(sRet)) {
return "OK";
} else {
mCol.getMedia().forceResync();
return sRet;
}
}
use of com.ichi2.anki.exception.NoEnoughServerSpaceException in project AnkiChinaAndroid by ankichinateam.
the class Syncer method sync.
/**
* Returns 'noChanges', 'fullSync', 'success', etc
*/
// public Object[] sync() throws UnknownHttpResponseException {
// return sync(null);
// }
public Object[] sync(Connection con, long restSpace) throws UnknownHttpResponseException, NoEnoughServerSpaceException {
mSyncMsg = "";
setRestSpace(restSpace);
// if the deck has any pending changes, flush them first and bump mod time
mCol.getSched()._updateCutoff();
mCol.save();
// step 1: login & metadata
Response ret = mServer.meta();
if (ret == null) {
return null;
}
int returntype = ret.code();
if (returntype == 403) {
return new Object[] { "badAuth" };
}
try {
mCol.getDb().getDatabase().beginTransaction();
try {
Timber.i("Sync: getting meta data from server");
JSONObject rMeta = new JSONObject(ret.body().string());
mCol.log("rmeta", rMeta);
mSyncMsg = rMeta.getString("msg");
if (!rMeta.getBoolean("cont")) {
// Don't add syncMsg; it can be fetched by UI code using the accessor
return new Object[] { "serverAbort" };
} else {
// don't abort, but ui should show messages after sync finishes
// and require confirmation if it's non-empty
}
throwExceptionIfCancelled(con);
long rscm = rMeta.getLong("scm");
int rts = rMeta.getInt("ts");
mRMod = rMeta.getLong("mod");
mMaxUsn = rMeta.getInt("usn");
// skip uname, AnkiDroid already stores and shows it
trySetHostNum(rMeta);
Timber.i("Sync: building local meta data");
JSONObject lMeta = meta();
mCol.log("lmeta", lMeta);
mLMod = lMeta.getLong("mod");
mMinUsn = lMeta.getInt("usn");
long lscm = lMeta.getLong("scm");
int lts = lMeta.getInt("ts");
long diff = Math.abs(rts - lts);
if (diff > 300) {
mCol.log("clock off");
return new Object[] { "clockOff", diff };
}
if (mLMod == mRMod) {
Timber.i("Sync: no changes - returning");
mCol.log("no changes");
return new Object[] { "noChanges" };
} else if (lscm != rscm) {
Timber.i("Sync: full sync necessary - returning");
mCol.log("schema diff");
return new Object[] { "fullSync" };
}
mLNewer = mLMod > mRMod;
// step 1.5: check collection is valid
if (!mCol.basicCheck()) {
mCol.log("basic check");
return new Object[] { "basicCheckFailed" };
}
throwExceptionIfCancelled(con);
// step 2: deletions
publishProgress(con, R.string.sync_deletions_message);
Timber.i("Sync: collection removed data");
JSONObject lrem = removed();
JSONObject o = new JSONObject();
o.put("minUsn", mMinUsn);
o.put("lnewer", mLNewer);
o.put("graves", lrem);
Timber.i("Sync: sending and receiving removed data");
JSONObject rrem = mServer.start(o);
Timber.i("Sync: applying removed data");
throwExceptionIfCancelled(con);
remove(rrem);
// ... and small objects
publishProgress(con, R.string.sync_small_objects_message);
Timber.i("Sync: collection small changes");
JSONObject lchg = changes();
JSONObject sch = new JSONObject();
sch.put("changes", lchg);
Timber.i("Sync: sending and receiving small changes");
long needSize = sch.toString().length();
Timber.i("Sync: sending and receiving small changes size:%d", needSize);
if (needSize > restSpace && Consts.loginAnkiChina()) {
throwExceptionIfNoSpace(needSize, restSpace);
} else {
restSpace -= needSize;
setRestSpace(restSpace);
Timber.i("Sync: remain size %d after for small changes ", restSpace);
}
JSONObject rchg = mServer.applyChanges(sch);
throwExceptionIfCancelled(con);
Timber.i("Sync: merging small changes");
try {
mergeChanges(lchg, rchg);
} catch (UnexpectedSchemaChange e) {
mServer.abort();
_forceFullSync();
}
// step 3: stream large tables from server
publishProgress(con, R.string.sync_download_chunk);
while (true) {
throwExceptionIfCancelled(con);
Timber.i("Sync: downloading chunked data");
JSONObject chunk = mServer.chunk();
mCol.log("server chunk", chunk);
Timber.i("Sync: applying chunked data");
applyChunk(chunk);
if (chunk.getBoolean("done")) {
break;
}
}
// step 4: stream to server
publishProgress(con, R.string.sync_upload_chunk);
List<JSONObject> sechs = new ArrayList<>();
long chunkSize = 0;
while (true) {
throwExceptionIfCancelled(con);
Timber.i("Sync: collecting chunked data");
JSONObject chunk = chunk();
mCol.log("client chunk", chunk);
JSONObject sech = new JSONObject();
sech.put("chunk", chunk);
chunkSize += sech.toString().length();
sechs.add(sech);
if (chunk.getBoolean("done")) {
break;
}
}
Timber.i("Sync: sending chunked data:%d", chunkSize);
if (chunkSize > restSpace && Consts.loginAnkiChina()) {
throwExceptionIfNoSpace(chunkSize, restSpace);
} else {
restSpace -= chunkSize;
setRestSpace(restSpace);
}
for (JSONObject object : sechs) {
mServer.applyChunk(object);
}
// step 5: sanity check
JSONObject c = sanityCheck();
JSONObject sanity = mServer.sanityCheck2(c);
if (sanity == null || !"ok".equals(sanity.optString("status", "bad"))) {
mCol.log("sanity check failed", c, sanity);
return _forceFullSync();
}
// finalize
publishProgress(con, R.string.sync_finish_message);
Timber.i("Sync: sending finish command");
long mod = mServer.finish();
if (mod == 0) {
return new Object[] { "finishError" };
}
Timber.i("Sync: finishing");
finish(mod);
publishProgress(con, R.string.sync_writing_db);
mCol.getDb().getDatabase().setTransactionSuccessful();
} catch (NoEnoughServerSpaceException e) {
// mCol.getDb().getDatabase().endTransaction();
throw new NoEnoughServerSpaceException(e.rest, e.need);
// e.printStackTrace();
// mCol.getDb().getDatabase().endTransaction();
// return new Object[] {"noServerSpace", e.rest, e.need};
} finally {
mCol.getDb().getDatabase().endTransaction();
}
} catch (NoEnoughServerSpaceException e) {
Timber.e("NoEnoughServerSpaceException ");
throw new NoEnoughServerSpaceException(e.rest, e.need);
// e.printStackTrace();
// mCol.getDb().getDatabase().endTransaction();
// return new Object[] {"noServerSpace", e.rest, e.need};
} catch (IllegalStateException e) {
throw new RuntimeException(e);
} catch (OutOfMemoryError e) {
AnkiDroidApp.sendExceptionReport(e, "Syncer-sync");
return new Object[] { "OutOfMemoryError" };
} catch (IOException e) {
AnkiDroidApp.sendExceptionReport(e, "Syncer-sync");
return new Object[] { "IOException" };
}
return new Object[] { "success", restSpace };
}
use of com.ichi2.anki.exception.NoEnoughServerSpaceException in project AnkiChinaAndroid by ankichinateam.
the class FullSyncer method upload.
@Override
public Object[] upload(long restSpace) throws UnknownHttpResponseException, NoEnoughServerSpaceException {
// make sure it's ok before we try to upload
mCon.publishProgress(R.string.sync_check_upload_file);
if (!"ok".equalsIgnoreCase(mCol.getDb().queryString("PRAGMA integrity_check"))) {
return new Object[] { "dbError" };
}
if (!mCol.basicCheck()) {
return new Object[] { "dbError" };
}
// apply some adjustments, then upload
mCol.beforeUpload();
String filePath = mCol.getPath();
double totalSize = FileUtil.getFileOrFilesSize(filePath, FileUtil.SIZETYPE_B);
Timber.i("full sync path size:%f", totalSize);
if (restSpace < totalSize && Consts.loginAnkiChina()) {
throw new NoEnoughServerSpaceException(restSpace, (long) totalSize);
}
Response ret;
mCon.publishProgress(R.string.sync_uploading_message);
try {
ret = super.req("upload", new FileInputStream(filePath));
if (ret == null || ret.body() == null) {
return null;
}
int status = ret.code();
if (status != 200) {
// error occurred
return new Object[] { "error", status, ret.message() };
} else {
return new Object[] { ret.body().string() };
}
} catch (IllegalStateException | IOException e) {
throw new RuntimeException(e);
}
}
use of com.ichi2.anki.exception.NoEnoughServerSpaceException in project AnkiChinaAndroid by ankichinateam.
the class Connection method doInBackgroundSync.
private Payload doInBackgroundSync(Payload data) {
sIsCancellable = true;
Timber.d("doInBackgroundSync()");
// Block execution until any previous background task finishes, or timeout after 5s
boolean ok = CollectionTask.waitToFinish(5);
String hkey = (String) data.data[0];
boolean media = (Boolean) data.data[1];
String conflictResolution = (String) data.data[2];
HostNum hostNum = (HostNum) data.data[3];
long restServerSpace = (long) data.data[4];
// Use safe version that catches exceptions so that full sync is still possible
Collection col = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
boolean colCorruptFullSync = false;
if (!CollectionHelper.getInstance().colIsOpen() || !ok) {
if (conflictResolution != null && "download".equals(conflictResolution)) {
colCorruptFullSync = true;
} else {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
}
try {
CollectionHelper.getInstance().lockCollection();
HttpSyncer server = new RemoteServer(this, hkey, hostNum);
Syncer client = new Syncer(col, server, hostNum);
// run sync and check state
boolean noChanges = false;
if (conflictResolution == null) {
Timber.i("Sync - starting sync");
publishProgress(R.string.sync_prepare_syncing);
Object[] ret = client.sync(this, restServerSpace);
data.message = client.getSyncMsg();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
String retCode = (String) ret[0];
if (!"noChanges".equals(retCode) && !"success".equals(retCode)) {
data.success = false;
data.result = ret;
// Check if there was a sanity check error
if ("sanityCheckError".equals(retCode)) {
// Force full sync next time
col.modSchemaNoCheck();
col.save();
}
return data;
}
// save and note success state
if ("noChanges".equals(retCode)) {
// publishProgress(R.string.sync_no_changes_message);
noChanges = true;
}
restServerSpace = client.getRestSpace();
} else {
try {
// Disable sync cancellation for full-sync
sIsCancellable = false;
server = new FullSyncer(col, hkey, this, hostNum);
if ("upload".equals(conflictResolution)) {
Timber.i("Sync - fullsync - upload collection");
publishProgress(R.string.sync_preparing_full_sync_message);
Object[] ret = server.upload(restServerSpace);
col.reopen();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
if (!ret[0].equals(HttpSyncer.ANKIWEB_STATUS_OK)) {
data.success = false;
data.result = ret;
return data;
}
} else if ("download".equals(conflictResolution)) {
Timber.i("Sync - fullsync - download collection");
publishProgress(R.string.sync_downloading_message);
Object[] ret = server.download();
if (ret == null) {
Timber.w("Sync - fullsync - unknown error");
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
if ("success".equals(ret[0])) {
data.success = true;
col.reopen();
}
if (!"success".equals(ret[0])) {
Timber.w("Sync - fullsync - download failed");
data.success = false;
data.result = ret;
if (!colCorruptFullSync) {
col.reopen();
}
return data;
}
}
} catch (OutOfMemoryError e) {
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
data.success = false;
data.result = new Object[] { "OutOfMemoryError" };
return data;
} catch (RuntimeException e) {
if (timeoutOccurred(e)) {
data.result = new Object[] { "connectionError", e };
} else if ("UserAbortedSync".equals(e.getMessage())) {
data.result = new Object[] { "UserAbortedSync", e };
} else {
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
data.result = new Object[] { "IOException", e };
}
data.success = false;
return data;
}
}
// clear undo to avoid non syncing orphans (because undo resets usn too
if (!noChanges) {
col.clearUndo();
}
// then move on to media sync
sIsCancellable = true;
boolean noMediaChanges = false;
boolean enoughServerSpace = true;
String mediaError = null;
if (media) {
server = new RemoteMediaServer(col, hkey, this, hostNum);
MediaSyncer mediaClient = new MediaSyncer(col, (RemoteMediaServer) server, this);
String ret;
try {
// ������ܻ���Ϊ�ϴ��ļ������ռ��С���׳��쳣���ڴ�֮ǰ��Ҫ����ʣ��ռ��С
ret = mediaClient.sync(restServerSpace);
Timber.e("sync media ret is null");
if (ret == null) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error);
} else {
if ("corruptMediaDB".equals(ret)) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_db_error);
noMediaChanges = true;
}
if ("noChanges".equals(ret)) {
publishProgress(R.string.sync_media_no_changes);
noMediaChanges = true;
}
if ("sanityFailed".equals(ret)) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_sanity_failed);
} else {
publishProgress(R.string.sync_media_success);
}
}
} catch (RuntimeException e) {
if (timeoutOccurred(e)) {
data.result = new Object[] { "connectionError", e };
} else if ("UserAbortedSync".equals(e.getMessage())) {
data.result = new Object[] { "UserAbortedSync", e };
}
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error) + "\n\n" + e.getLocalizedMessage();
}
}
if (noChanges && (!media || noMediaChanges)) {
data.success = false;
data.result = new Object[] { "noChanges" };
return data;
} else {
data.success = true;
data.data = new Object[] { conflictResolution, col, mediaError };
return data;
}
} catch (MediaSyncException e) {
Timber.e("Media sync rejected by server");
data.success = false;
data.result = new Object[] { "mediaSyncServerError", e };
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync");
return data;
} catch (UnknownHttpResponseException e) {
Timber.e(e, "doInBackgroundSync -- unknown response code error");
data.success = false;
int code = e.getResponseCode();
String msg = e.getLocalizedMessage();
data.result = new Object[] { "error", code, msg };
return data;
} catch (NoEnoughServerSpaceException e) {
Timber.e("NoEnoughServerSpaceException ");
e.printStackTrace();
data.success = false;
data.result = new Object[] { "noServerSpace", e.rest, e.need };
return data;
} catch (Exception e) {
// Global error catcher.
// Try to give a human readable error, otherwise print the raw error message
Timber.e(e, "doInBackgroundSync error");
data.success = false;
if (timeoutOccurred(e)) {
data.result = new Object[] { "connectionError", e };
} else if ("UserAbortedSync".equals(e.getMessage())) {
data.result = new Object[] { "UserAbortedSync", e };
} else {
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync");
data.result = new Object[] { e.getLocalizedMessage(), e };
}
return data;
} finally {
Timber.i("Sync Finished - Closing Collection");
// don't bump mod time unless we explicitly save
if (col != null) {
col.close(false);
}
CollectionHelper.getInstance().unlockCollection();
}
}
Aggregations