internal SaveNamespaceContext(FSNamesystem sourceNamesystem, long txid, Canceler canceller) { this.sourceNamesystem = sourceNamesystem; this.txid = txid; this.canceller = canceller; }
/// <summary> /// 操作キャンセル /// </summary> /// <param name="context"></param> /// <param name="text"></param> /// <returns></returns> public async Task TryCatch(IDialogContext context, string text, Canceler cancel) { //キャンセルを判断する変数 CancellationTokenSource source = new CancellationTokenSource(); try { //キャンセルが入力されたら if (text.IsAny("キャンセル", "exit", "キャンセルする")) { //キャンセル変数にキャンセルを格納する source.Cancel(); //これ以降非同期処理が行われたらOperationCanceledExceptionをthrowするようにする source.Token.ThrowIfCancellationRequested(); } //なにかしらの処理 await cancel(); } //操作キャンセル時の処理 catch (OperationCanceledException) { //操作がキャンセルされたというメッセージを出す await context.PostAsync("操作は取り消されました"); //メッセージ送信待機 context.Wait(MessageReceivedAsync); } //ユーザ情報不正時の処理 catch (AuthorizationException) { //ユーザ情報が不正というメッセージを出す await context.PostAsync("不正なユーザ情報です"); //メッセージ送信待機 context.Wait(MessageReceivedAsync); } //それ以外の例外処理 catch (Exception) { //ユーザ情報が不正というメッセージを出す await context.PostAsync("不正な処理が発生しました"); //メッセージ送信待機 context.Wait(MessageReceivedAsync); } }
/// <exception cref="System.IO.FileNotFoundException"/> /// <exception cref="System.IO.IOException"/> private static void WriteFileToPutRequest(Configuration conf, HttpURLConnection connection , FilePath imageFile, Canceler canceler) { connection.SetRequestProperty(ContentType, "application/octet-stream"); connection.SetRequestProperty(ContentTransferEncoding, "binary"); OutputStream output = connection.GetOutputStream(); FileInputStream input = new FileInputStream(imageFile); try { CopyFileToStream(output, imageFile, input, ImageServlet.GetThrottler(conf), canceler ); } finally { IOUtils.CloseStream(input); IOUtils.CloseStream(output); } }
private void StartClosed() { string xmlString = base.GetXML(); Visible = false; string sqlString = Options.GetSQL(xmlString); //клас для принудительного завершения ассинхронной операции var cancel = new Canceler(); Cursor = Cursors.WaitCursor; //запуск функции, котрая сначала выключит все формы, потом запускает ассинхронную операцию, по окончанию которой формы будут включены, эту ассинхронную операцию завершает либо таймер, либо окончание поиска //функиция приостанавливает поток, пока не будет реально запущена ассинхронная операция ожидания windowsEnabler.WindowsWait(SynchronizationContext.Current, cancel); SearchDocsDelegate searchBegin = Environment.DocData.SearchDocs; searchBegin.BeginInvoke(Environment.DocData.GetFoundDocsIDQuery(sqlString, true), base.GetInFound(), base.GetToFound(), Environment.CurEmp.ID, Forms.MainFormDialog.maxSearchResults, (IAsyncResult ar) => { var searchBegindel = ar.AsyncState as SearchDocsDelegate; if (searchBegindel != null) { searchBegindel.EndInvoke(ar); } cancel.Cancel(); //ожидание завершения WindowsWait//возможно нужно ждать завершения включения окон windowsEnabler.AsyncWaitHandleClose.WaitOne(2000); canClose = true; if (InvokeRequired) { Invoke((MethodInvoker)(Close)); } else { Close(); } }, searchBegin); }
/// <summary> /// エラーハンドラー /// </summary> /// <param name="data"></param> /// <param name="cancel"></param> /// <returns></returns> public static async Task Handler(NameValueCollection data, Canceler cancel) { try { //なにかしらの処理 await cancel(); } //操作キャンセル時の処理 catch (OperationCanceledException) { } //ユーザ情報不正時の処理 catch (AuthorizationException) { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "ユーザ情報が不正、もしくは存在しません。", Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); } //それ以外の例外処理 catch (Exception) { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "予期せぬエラーが発生しました。", Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); } }
/// <exception cref="System.IO.IOException"/> private static void CopyFileToStream(OutputStream @out, FilePath localfile, FileInputStream infile, DataTransferThrottler throttler, Canceler canceler) { byte[] buf = new byte[HdfsConstants.IoFileBufferSize]; try { CheckpointFaultInjector.GetInstance().AboutToSendFile(localfile); if (CheckpointFaultInjector.GetInstance().ShouldSendShortFile(localfile)) { // Test sending image shorter than localfile long len = localfile.Length(); buf = new byte[(int)Math.Min(len / 2, HdfsConstants.IoFileBufferSize)]; // This will read at most half of the image // and the rest of the image will be sent over the wire infile.Read(buf); } int num = 1; while (num > 0) { if (canceler != null && canceler.IsCancelled()) { throw new SaveNamespaceCancelledException(canceler.GetCancellationReason()); } num = infile.Read(buf); if (num <= 0) { break; } if (CheckpointFaultInjector.GetInstance().ShouldCorruptAByte(localfile)) { // Simulate a corrupted byte on the wire Log.Warn("SIMULATING A CORRUPT BYTE IN IMAGE TRANSFER!"); buf[0]++; } @out.Write(buf, 0, num); if (throttler != null) { throttler.Throttle(num, canceler); } } } catch (EofException) { Log.Info("Connection closed by client"); @out = null; } finally { // so we don't close in the finally if (@out != null) { @out.Close(); } } }
/* * Uploads the imagefile using HTTP PUT method */ /// <exception cref="System.IO.IOException"/> private static void UploadImage(Uri url, Configuration conf, NNStorage storage, NNStorage.NameNodeFile nnf, long txId, Canceler canceler) { FilePath imageFile = storage.FindImageFile(nnf, txId); if (imageFile == null) { throw new IOException("Could not find image with txid " + txId); } HttpURLConnection connection = null; try { URIBuilder uriBuilder = new URIBuilder(url.ToURI()); // write all params for image upload request as query itself. // Request body contains the image to be uploaded. IDictionary <string, string> @params = ImageServlet.GetParamsForPutImage(storage, txId, imageFile.Length(), nnf); foreach (KeyValuePair <string, string> entry in @params) { uriBuilder.AddParameter(entry.Key, entry.Value); } Uri urlWithParams = uriBuilder.Build().ToURL(); connection = (HttpURLConnection)connectionFactory.OpenConnection(urlWithParams, UserGroupInformation .IsSecurityEnabled()); // Set the request to PUT connection.SetRequestMethod("PUT"); connection.SetDoOutput(true); int chunkSize = conf.GetInt(DFSConfigKeys.DfsImageTransferChunksizeKey, DFSConfigKeys .DfsImageTransferChunksizeDefault); if (imageFile.Length() > chunkSize) { // using chunked streaming mode to support upload of 2GB+ files and to // avoid internal buffering. // this mode should be used only if more than chunkSize data is present // to upload. otherwise upload may not happen sometimes. connection.SetChunkedStreamingMode(chunkSize); } SetTimeout(connection); // set headers for verification ImageServlet.SetVerificationHeadersForPut(connection, imageFile); // Write the file to output stream. WriteFileToPutRequest(conf, connection, imageFile, canceler); int responseCode = connection.GetResponseCode(); if (responseCode != HttpURLConnection.HttpOk) { throw new TransferFsImage.HttpPutFailedException(string.Format("Image uploading failed, status: %d, url: %s, message: %s" , responseCode, urlWithParams, connection.GetResponseMessage()), responseCode); } } catch (AuthenticationException e) { throw new IOException(e); } catch (URISyntaxException e) { throw new IOException(e); } finally { if (connection != null) { connection.Disconnect(); } } }
/// <summary>Requests that the NameNode download an image from this node.</summary> /// <remarks> /// Requests that the NameNode download an image from this node. Allows for /// optional external cancelation. /// </remarks> /// <param name="fsName">the http address for the remote NN</param> /// <param name="conf">Configuration</param> /// <param name="storage">the storage directory to transfer the image from</param> /// <param name="nnf">the NameNodeFile type of the image</param> /// <param name="txid">the transaction ID of the image to be uploaded</param> /// <param name="canceler">optional canceler to check for abort of upload</param> /// <exception cref="System.IO.IOException">if there is an I/O error or cancellation</exception> public static void UploadImageFromStorage(Uri fsName, Configuration conf, NNStorage storage, NNStorage.NameNodeFile nnf, long txid, Canceler canceler) { Uri url = new Uri(fsName, ImageServlet.PathSpec); long startTime = Time.MonotonicNow(); try { UploadImage(url, conf, storage, nnf, txid, canceler); } catch (TransferFsImage.HttpPutFailedException e) { if (e.GetResponseCode() == HttpServletResponse.ScConflict) { // this is OK - this means that a previous attempt to upload // this checkpoint succeeded even though we thought it failed. Log.Info("Image upload with txid " + txid + " conflicted with a previous image upload to the " + "same NameNode. Continuing...", e); return; } else { throw; } } double xferSec = Math.Max(((float)(Time.MonotonicNow() - startTime)) / 1000.0, 0.001 ); Log.Info("Uploaded image with txid " + txid + " to namenode at " + fsName + " in " + xferSec + " seconds"); }
public void WindowsWait(SynchronizationContext context, Canceler cancel) { AsyncWaitHandleCreate.Reset(); AsyncWaitHandleClose.Reset(); //выражение, включающее - выключающее формы Action <Boolean> switchingWindows = (Boolean enable) => { foreach (System.Windows.Forms.Form window in System.Windows.Forms.Application.OpenForms) { if (window.Visible) { if (enable) { window.Cursor = System.Windows.Forms.Cursors.Default; } else { window.Cursor = System.Windows.Forms.Cursors.WaitCursor; } window.Enabled = enable; } } }; context.Send((object state) => switchingWindows(false), null); //ассинхронная операция, выполняющая ожидание WaiterDelegate waiterAction = (SynchronizationContext graphicsContext, Canceler canceler) => { AsyncWaitHandleCreate.Set(); Int32 counter = 0; try { do { if (canceler.ThrowIfCancellationRequested()) { break; } Thread.Sleep(50); //20HZ процесс } while((COUNTER_MAX * 20) >= ++counter); } catch (OperationCanceledException ex) { Lib.Win.Data.Env.WriteToLog(ex); } AsyncWaitHandleClose.Set(); return(graphicsContext); }; waiterAction.BeginInvoke(context, cancel, (IAsyncResult ar) => { var waiterActionEnd = ar.AsyncState as WaiterDelegate; if (waiterActionEnd != null) { waiterActionEnd.EndInvoke(ar); } if (ar.CompletedSynchronously) { switchingWindows(true); } else { context.Send( (object state) => switchingWindows(true), null); } }, waiterAction); //ожидание запуска waiterAction AsyncWaitHandleCreate.WaitOne(5000); }
public _Callable_572(Canceler canceler) { this.canceler = canceler; }
public _Callable_561(FSImage image, FSNamesystem finalFsn, Canceler canceler) { this.image = image; this.finalFsn = finalFsn; this.canceler = canceler; }
/// <exception cref="System.Exception"/> public virtual void TestCancelSaveNamespace() { Configuration conf = GetConf(); NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode); DFSTestUtil.FormatNameNode(conf); FSNamesystem fsn = FSNamesystem.LoadFromDisk(conf); // Replace the FSImage with a spy FSImage image = fsn.GetFSImage(); NNStorage storage = image.GetStorage(); storage.Close(); // unlock any directories that FSNamesystem's initialization may have locked storage.SetStorageDirectories(FSNamesystem.GetNamespaceDirs(conf), FSNamesystem.GetNamespaceEditsDirs (conf)); FSNamesystem spyFsn = Org.Mockito.Mockito.Spy(fsn); FSNamesystem finalFsn = spyFsn; GenericTestUtils.DelayAnswer delayer = new GenericTestUtils.DelayAnswer(Log); BlockIdManager bid = Org.Mockito.Mockito.Spy(spyFsn.GetBlockIdManager()); Whitebox.SetInternalState(finalFsn, "blockIdManager", bid); Org.Mockito.Mockito.DoAnswer(delayer).When(bid).GetGenerationStampV2(); ExecutorService pool = Executors.NewFixedThreadPool(2); try { DoAnEdit(fsn, 1); Canceler canceler = new Canceler(); // Save namespace fsn.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter); try { Future <Void> saverFuture = pool.Submit(new _Callable_561(image, finalFsn, canceler )); // Wait until saveNamespace calls getGenerationStamp delayer.WaitForCall(); // then cancel the saveNamespace Future <Void> cancelFuture = pool.Submit(new _Callable_572(canceler)); // give the cancel call time to run Sharpen.Thread.Sleep(500); // allow saveNamespace to proceed - it should check the cancel flag after // this point and throw an exception delayer.Proceed(); cancelFuture.Get(); saverFuture.Get(); NUnit.Framework.Assert.Fail("saveNamespace did not fail even though cancelled!"); } catch (Exception t) { GenericTestUtils.AssertExceptionContains("SaveNamespaceCancelledException", t); } Log.Info("Successfully cancelled a saveNamespace"); // Check that we have only the original image and not any // cruft left over from half-finished images FSImageTestUtil.LogStorageContents(Log, storage); foreach (Storage.StorageDirectory sd in storage.DirIterable(null)) { FilePath curDir = sd.GetCurrentDir(); GenericTestUtils.AssertGlobEquals(curDir, "fsimage_.*", NNStorage.GetImageFileName (0), NNStorage.GetImageFileName(0) + MD5FileUtils.Md5Suffix); } } finally { fsn.Close(); } }