[Lazy1] Switch to blocking log closures

Change-Id: I4daec20b7b47b0d71c5aab6e17cd660015e19e71
main
Jean Chalard 2013-02-14 14:03:24 -08:00 committed by Kurt Partridge
parent bba39b9b67
commit 7423005b96
2 changed files with 45 additions and 42 deletions

View File

@ -20,11 +20,11 @@ import android.content.Context;
import android.util.JsonWriter; import android.util.JsonWriter;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
@ -54,7 +54,6 @@ public class ResearchLog {
private static final String TAG = ResearchLog.class.getSimpleName(); private static final String TAG = ResearchLog.class.getSimpleName();
private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG; private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG;
private static final long FLUSH_DELAY_IN_MS = 1000 * 5; private static final long FLUSH_DELAY_IN_MS = 1000 * 5;
private static final int ABORT_TIMEOUT_IN_MS = 1000 * 4;
/* package */ final ScheduledExecutorService mExecutor; /* package */ final ScheduledExecutorService mExecutor;
/* package */ final File mFile; /* package */ final File mFile;
@ -100,7 +99,7 @@ public class ResearchLog {
* *
* See class comment for details about {@code JsonWriter} construction. * See class comment for details about {@code JsonWriter} construction.
*/ */
public synchronized void close(final Runnable onClosed) { private synchronized void close(final Runnable onClosed) {
mExecutor.submit(new Callable<Object>() { mExecutor.submit(new Callable<Object>() {
@Override @Override
public Object call() throws Exception { public Object call() throws Exception {
@ -131,15 +130,22 @@ public class ResearchLog {
mExecutor.shutdown(); mExecutor.shutdown();
} }
private boolean mIsAbortSuccessful; /**
* Block until the research log has shut down and spooled out all output or {@code timeout}
* occurs.
*
* @param timeout time to wait for close in milliseconds
*/
public void blockingClose(final long timeout) {
close(null);
awaitTermination(timeout, TimeUnit.MILLISECONDS);
}
/** /**
* Waits for publication requests to finish, closes the {@link JsonWriter}, but then deletes the * Waits for publication requests to finish, closes the JsonWriter, but then deletes the backing
* backing file used for output. * output file.
*
* See class comment for details about {@code JsonWriter} construction.
*/ */
public synchronized void abort() { private synchronized void abort() {
mExecutor.submit(new Callable<Object>() { mExecutor.submit(new Callable<Object>() {
@Override @Override
public Object call() throws Exception { public Object call() throws Exception {
@ -151,7 +157,7 @@ public class ResearchLog {
} }
} finally { } finally {
if (mFile != null) { if (mFile != null) {
mIsAbortSuccessful = mFile.delete(); mFile.delete();
} }
} }
return null; return null;
@ -161,14 +167,25 @@ public class ResearchLog {
mExecutor.shutdown(); mExecutor.shutdown();
} }
public boolean blockingAbort() throws InterruptedException { /**
* Block until the research log has aborted or {@code timeout} occurs.
*
* @param timeout time to wait for close in milliseconds
*/
public void blockingAbort(final long timeout) {
abort(); abort();
mExecutor.awaitTermination(ABORT_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS); awaitTermination(timeout, TimeUnit.MILLISECONDS);
return mIsAbortSuccessful;
} }
public void awaitTermination(int delay, TimeUnit timeUnit) throws InterruptedException { @UsedForTesting
mExecutor.awaitTermination(delay, timeUnit); public void awaitTermination(final long delay, final TimeUnit timeUnit) {
try {
if (!mExecutor.awaitTermination(delay, timeUnit)) {
Log.e(TAG, "ResearchLog executor timed out while awaiting terminaion");
}
} catch (final InterruptedException e) {
Log.e(TAG, "ResearchLog executor interrupted while awaiting terminaion", e);
}
} }
/* package */ synchronized void flush() { /* package */ synchronized void flush() {

View File

@ -154,6 +154,9 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
private static final int MAX_INPUTVIEW_LENGTH_TO_CAPTURE = 8192; // must be >=1 private static final int MAX_INPUTVIEW_LENGTH_TO_CAPTURE = 8192; // must be >=1
private static final String PREF_RESEARCH_SAVED_CHANNEL = "pref_research_saved_channel"; private static final String PREF_RESEARCH_SAVED_CHANNEL = "pref_research_saved_channel";
private static final long RESEARCHLOG_CLOSE_TIMEOUT_IN_MS = 5 * 1000;
private static final long RESEARCHLOG_ABORT_TIMEOUT_IN_MS = 5 * 1000;
private static final ResearchLogger sInstance = new ResearchLogger(); private static final ResearchLogger sInstance = new ResearchLogger();
private static String sAccountType = null; private static String sAccountType = null;
private static String sAllowedAccountDomain = null; private static String sAllowedAccountDomain = null;
@ -502,42 +505,29 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
commitCurrentLogUnit(); commitCurrentLogUnit();
mMainLogBuffer.setIsStopping(); mMainLogBuffer.setIsStopping();
mMainLogBuffer.shiftAndPublishAll(); mMainLogBuffer.shiftAndPublishAll();
mMainResearchLog.close(null /* callback */); mMainResearchLog.blockingClose(RESEARCHLOG_CLOSE_TIMEOUT_IN_MS);
mMainLogBuffer = null; mMainLogBuffer = null;
} }
if (mFeedbackLogBuffer != null) { if (mFeedbackLogBuffer != null) {
mFeedbackLog.close(null /* callback */); mFeedbackLog.blockingClose(RESEARCHLOG_CLOSE_TIMEOUT_IN_MS);
mFeedbackLogBuffer = null; mFeedbackLogBuffer = null;
} }
} }
public boolean abort() { public void abort() {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "abort called"); Log.d(TAG, "abort called");
} }
boolean didAbortMainLog = false;
if (mMainLogBuffer != null) { if (mMainLogBuffer != null) {
mMainLogBuffer.clear(); mMainLogBuffer.clear();
try { mMainResearchLog.blockingAbort(RESEARCHLOG_ABORT_TIMEOUT_IN_MS);
didAbortMainLog = mMainResearchLog.blockingAbort();
} catch (InterruptedException e) {
// Don't know whether this succeeded or not. We assume not; this is reported
// to the caller.
}
mMainLogBuffer = null; mMainLogBuffer = null;
} }
boolean didAbortFeedbackLog = false;
if (mFeedbackLogBuffer != null) { if (mFeedbackLogBuffer != null) {
mFeedbackLogBuffer.clear(); mFeedbackLogBuffer.clear();
try { mFeedbackLog.blockingAbort(RESEARCHLOG_ABORT_TIMEOUT_IN_MS);
didAbortFeedbackLog = mFeedbackLog.blockingAbort();
} catch (InterruptedException e) {
// Don't know whether this succeeded or not. We assume not; this is reported
// to the caller.
}
mFeedbackLogBuffer = null; mFeedbackLogBuffer = null;
} }
return didAbortMainLog && didAbortFeedbackLog;
} }
private void restart() { private void restart() {
@ -620,7 +610,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
private void startRecordingInternal() { private void startRecordingInternal() {
if (mUserRecordingLog != null) { if (mUserRecordingLog != null) {
mUserRecordingLog.abort(); mUserRecordingLog.blockingAbort(RESEARCHLOG_ABORT_TIMEOUT_IN_MS);
} }
mUserRecordingFile = createUserRecordingFile(mFilesDir); mUserRecordingFile = createUserRecordingFile(mFilesDir);
mUserRecordingLog = new ResearchLog(mUserRecordingFile, mLatinIME); mUserRecordingLog = new ResearchLog(mUserRecordingFile, mLatinIME);
@ -658,7 +648,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
private void cancelRecording() { private void cancelRecording() {
if (mUserRecordingLog != null) { if (mUserRecordingLog != null) {
mUserRecordingLog.abort(); mUserRecordingLog.blockingAbort(RESEARCHLOG_ABORT_TIMEOUT_IN_MS);
} }
mUserRecordingLog = null; mUserRecordingLog = null;
mUserRecordingLogBuffer = null; mUserRecordingLogBuffer = null;
@ -670,7 +660,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
private void saveRecording() { private void saveRecording() {
commitCurrentLogUnit(); commitCurrentLogUnit();
publishLogBuffer(mUserRecordingLogBuffer, mUserRecordingLog, true); publishLogBuffer(mUserRecordingLogBuffer, mUserRecordingLog, true);
mUserRecordingLog.close(null); mUserRecordingLog.blockingClose(RESEARCHLOG_CLOSE_TIMEOUT_IN_MS);
mUserRecordingLog = null; mUserRecordingLog = null;
mUserRecordingLogBuffer = null; mUserRecordingLogBuffer = null;
@ -782,12 +772,8 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
feedbackContents, accountName, recording); feedbackContents, accountName, recording);
mFeedbackLogBuffer.shiftIn(feedbackLogUnit); mFeedbackLogBuffer.shiftIn(feedbackLogUnit);
publishLogBuffer(mFeedbackLogBuffer, mSavedFeedbackLog, true /* isIncludingPrivateData */); publishLogBuffer(mFeedbackLogBuffer, mSavedFeedbackLog, true /* isIncludingPrivateData */);
mSavedFeedbackLog.close(new Runnable() { mSavedFeedbackLog.blockingClose(RESEARCHLOG_CLOSE_TIMEOUT_IN_MS);
@Override uploadNow();
public void run() {
uploadNow();
}
});
if (isIncludingRecording && DEBUG_REPLAY_AFTER_FEEDBACK) { if (isIncludingRecording && DEBUG_REPLAY_AFTER_FEEDBACK) {
final Handler handler = new Handler(); final Handler handler = new Handler();