Beispiel #1
0
        public virtual void TestRenameMapOutputForReduce()
        {
            JobConf       conf          = new JobConf();
            MROutputFiles mrOutputFiles = new MROutputFiles();

            mrOutputFiles.SetConf(conf);
            // make sure both dirs are distinct
            //
            conf.Set(MRConfig.LocalDir, localDirs[0].ToString());
            Path mapOut = mrOutputFiles.GetOutputFileForWrite(1);

            conf.Set(MRConfig.LocalDir, localDirs[1].ToString());
            Path mapOutIdx = mrOutputFiles.GetOutputIndexFileForWrite(1);

            Assert.AssertNotEquals("Paths must be different!", mapOut.GetParent(), mapOutIdx.
                                   GetParent());
            // make both dirs part of LOCAL_DIR
            conf.SetStrings(MRConfig.LocalDir, localDirs);
            FileContext lfc = FileContext.GetLocalFSFileContext(conf);

            lfc.Create(mapOut, EnumSet.Of(CreateFlag.Create)).Close();
            lfc.Create(mapOutIdx, EnumSet.Of(CreateFlag.Create)).Close();
            JobId         jobId = MRBuilderUtils.NewJobId(12345L, 1, 2);
            TaskId        tid   = MRBuilderUtils.NewTaskId(jobId, 0, TaskType.Map);
            TaskAttemptId taid  = MRBuilderUtils.NewTaskAttemptId(tid, 0);

            LocalContainerLauncher.RenameMapOutputForReduce(conf, taid, mrOutputFiles);
        }
        /*
         * Create files with numBlocks blocks each with block size blockSize.
         */
        /// <exception cref="System.IO.IOException"/>
        public override long CreateFile(Path path, int numBlocks, params Options.CreateOpts
                                        [] options)
        {
            Options.CreateOpts.BlockSize blockSizeOpt = Options.CreateOpts.GetOpt <Options.CreateOpts.BlockSize
                                                                                   >(options);
            long blockSize = blockSizeOpt != null?blockSizeOpt.GetValue() : DefaultBlockSize;

            FSDataOutputStream @out = fc.Create(path, EnumSet.Of(CreateFlag.Create), options);

            byte[] data = GetFileData(numBlocks, blockSize);
            @out.Write(data, 0, data.Length);
            @out.Close();
            return(data.Length);
        }
Beispiel #3
0
        public virtual void TestWorkingDirectory()
        {
            // First we cd to our test root
            fc.Mkdir(new Path("/testWd"), FileContext.DefaultPerm, false);
            Path workDir = new Path("/testWd");
            Path fqWd    = fc.MakeQualified(workDir);

            fc.SetWorkingDirectory(workDir);
            Assert.Equal(fqWd, fc.GetWorkingDirectory());
            fc.SetWorkingDirectory(new Path("."));
            Assert.Equal(fqWd, fc.GetWorkingDirectory());
            fc.SetWorkingDirectory(new Path(".."));
            Assert.Equal(fqWd.GetParent(), fc.GetWorkingDirectory());
            // cd using a relative path
            // Go back to our test root
            workDir = new Path("/testWd");
            fqWd    = fc.MakeQualified(workDir);
            fc.SetWorkingDirectory(workDir);
            Assert.Equal(fqWd, fc.GetWorkingDirectory());
            Path relativeDir = new Path("existingDir1");
            Path absoluteDir = new Path(workDir, "existingDir1");

            fc.Mkdir(absoluteDir, FileContext.DefaultPerm, true);
            Path fqAbsoluteDir = fc.MakeQualified(absoluteDir);

            fc.SetWorkingDirectory(relativeDir);
            Assert.Equal(fqAbsoluteDir, fc.GetWorkingDirectory());
            // cd using a absolute path
            absoluteDir   = new Path("/test/existingDir2");
            fqAbsoluteDir = fc.MakeQualified(absoluteDir);
            fc.Mkdir(absoluteDir, FileContext.DefaultPerm, true);
            fc.SetWorkingDirectory(absoluteDir);
            Assert.Equal(fqAbsoluteDir, fc.GetWorkingDirectory());
            // Now open a file relative to the wd we just set above.
            Path absolutePath = new Path(absoluteDir, "foo");

            fc.Create(absolutePath, EnumSet.Of(CreateFlag.Create)).Close();
            fc.Open(new Path("foo")).Close();
            // Now mkdir relative to the dir we cd'ed to
            fc.Mkdir(new Path("newDir"), FileContext.DefaultPerm, true);
            Assert.True(FileContextTestHelper.IsDir(fc, new Path(absoluteDir
                                                                 , "newDir")));
            absoluteDir = fileContextTestHelper.GetTestRootPath(fc, "nonexistingPath");
            try
            {
                fc.SetWorkingDirectory(absoluteDir);
                NUnit.Framework.Assert.Fail("cd to non existing dir should have failed");
            }
            catch (Exception)
            {
            }
            // Exception as expected
            // Try a URI
            string LocalFsRootUri = "file:///tmp/test";

            absoluteDir = new Path(LocalFsRootUri + "/existingDir");
            fc.Mkdir(absoluteDir, FileContext.DefaultPerm, true);
            fc.SetWorkingDirectory(absoluteDir);
            Assert.Equal(absoluteDir, fc.GetWorkingDirectory());
        }
Beispiel #4
0
        public virtual void TestLinkTargetNonSymlink()
        {
            FileContext fc         = null;
            Path        notSymlink = new Path("/notasymlink");

            try
            {
                fc = FileContext.GetFileContext(cluster.GetFileSystem().GetUri());
                fc.Create(notSymlink, EnumSet.Of(CreateFlag.Create));
                DFSClient client = new DFSClient(cluster.GetFileSystem().GetUri(), cluster.GetConfiguration
                                                     (0));
                try
                {
                    client.GetLinkTarget(notSymlink.ToString());
                    NUnit.Framework.Assert.Fail("Expected exception for resolving non-symlink");
                }
                catch (IOException e)
                {
                    GenericTestUtils.AssertExceptionContains("is not a symbolic link", e);
                }
            }
            finally
            {
                if (fc != null)
                {
                    fc.Delete(notSymlink, false);
                }
            }
        }
Beispiel #5
0
        /// <exception cref="System.IO.IOException"/>
        internal virtual byte[] CreateTmpFile(Path dst, Random r, int len)
        {
            // use unmodified local context
            FileContext lfs = FileContext.GetLocalFSFileContext();

            dst = lfs.MakeQualified(dst);
            lfs.Mkdir(dst.GetParent(), null, true);
            byte[]             bytes = new byte[len];
            FSDataOutputStream @out  = null;

            try
            {
                @out = lfs.Create(dst, EnumSet.Of(CreateFlag.Create, CreateFlag.Overwrite));
                r.NextBytes(bytes);
                @out.Write(bytes);
            }
            finally
            {
                if (@out != null)
                {
                    @out.Close();
                }
            }
            return(bytes);
        }
Beispiel #6
0
        /// <exception cref="System.IO.IOException"/>
        private void CreateFile(Path path)
        {
            FSDataOutputStream @out = fc.Create(path, EnumSet.Of(CreateFlag.Create), Options.CreateOpts
                                                .CreateParent());

            @out.Write(data, 0, data.Length);
            @out.Close();
        }
Beispiel #7
0
        /// <exception cref="System.IO.IOException"/>
        public static void WriteFile(FileContext fc, Path path, byte[] b)
        {
            FSDataOutputStream @out = fc.Create(path, EnumSet.Of(CreateFlag.Create), Options.CreateOpts
                                                .CreateParent());

            @out.Write(b);
            @out.Close();
        }
Beispiel #8
0
        /// <summary>
        /// Create a file with the name <code>file</code> and
        /// a length of <code>fileSize</code>.
        /// </summary>
        /// <remarks>
        /// Create a file with the name <code>file</code> and
        /// a length of <code>fileSize</code>. The file is filled with character 'a'.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        private void GenFile(Path file, long fileSize)
        {
            FSDataOutputStream @out = fc.Create(file, EnumSet.Of(CreateFlag.Create, CreateFlag
                                                                 .Overwrite), Options.CreateOpts.CreateParent(), Options.CreateOpts.BufferSize(4096
                                                                                                                                               ), Options.CreateOpts.RepFac((short)3));

            for (long i = 0; i < fileSize; i++)
            {
                @out.WriteByte('a');
            }
            @out.Close();
        }
Beispiel #9
0
        /// <exception cref="System.IO.IOException"/>
        private static void WriteFile(FileContext fc, Path name, int fileSize)
        {
            // Create and write a file that contains three blocks of data
            FSDataOutputStream stm = fc.Create(name, EnumSet.Of(CreateFlag.Create), Options.CreateOpts
                                               .CreateParent());

            byte[] buffer = new byte[fileSize];
            Random rand   = new Random(seed);

            rand.NextBytes(buffer);
            stm.Write(buffer);
            stm.Close();
        }
Beispiel #10
0
        /// <exception cref="System.IO.IOException"/>
        public static void AppendToFile(FileContext fc, Path path, int numBlocks, params
                                        Options.CreateOpts[] options)
        {
            Options.CreateOpts.BlockSize blockSizeOpt = Options.CreateOpts.GetOpt <Options.CreateOpts.BlockSize
                                                                                   >(options);
            long blockSize = blockSizeOpt != null?blockSizeOpt.GetValue() : DefaultBlockSize;

            FSDataOutputStream @out;

            @out = fc.Create(path, EnumSet.Of(CreateFlag.Append));
            byte[] data = GetFileData(numBlocks, blockSize);
            @out.Write(data, 0, data.Length);
            @out.Close();
        }
Beispiel #11
0
        /// <exception cref="System.IO.IOException"/>
        internal static void CreateFile(FileContext files, Path p, int len, Random r)
        {
            FSDataOutputStream @out = null;

            try
            {
                byte[] bytes = new byte[len];
                @out = files.Create(p, EnumSet.Of(CreateFlag.Create, CreateFlag.Overwrite));
                r.NextBytes(bytes);
                @out.Write(bytes);
            }
            finally
            {
                if (@out != null)
                {
                    @out.Close();
                }
            }
        }
        public void CreateFileRecord()
        {
            UserContext _userContext = new UserContext();
            CUser       user         = _userContext.GetByName("Dmitrii");
            FileContext _fileContext = new FileContext();

            byte[] hash = Encoding.ASCII.GetBytes("0eff316809032f72e0237f8bcb1b65cb");

            SqlConnection myConnection = new SqlConnection();

            myConnection.ConnectionString = ConnectionContext.GetConnectionString();
            myConnection.Open();
            SqlTransaction myTransaction = myConnection.BeginTransaction("testTransaction");


            CFile cFile   = new CFile(Guid.NewGuid(), "testFile", "some/weird/path", 1024, user.Guid, true, hash, DateTime.Now, 0, 0, 0);
            int   created = _fileContext.Create(cFile, myConnection, myTransaction);

            Assert.AreEqual(created, 1);
        }
Beispiel #13
0
        public virtual int Call()
        {
            // dispatcher not typed
            ContainerLaunchContext launchContext = container.GetLaunchContext();
            IDictionary <Path, IList <string> > localResources = null;
            ContainerId    containerID    = container.GetContainerId();
            string         containerIdStr = ConverterUtils.ToString(containerID);
            IList <string> command        = launchContext.GetCommands();
            int            ret            = -1;

            // CONTAINER_KILLED_ON_REQUEST should not be missed if the container
            // is already at KILLING
            if (container.GetContainerState() == ContainerState.Killing)
            {
                dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType
                                                                           .ContainerKilledOnRequest, Shell.Windows ? ContainerExecutor.ExitCode.ForceKilled
                                                                           .GetExitCode() : ContainerExecutor.ExitCode.Terminated.GetExitCode(), "Container terminated before launch."
                                                                           ));
                return(0);
            }
            try
            {
                localResources = container.GetLocalizedResources();
                if (localResources == null)
                {
                    throw RPCUtil.GetRemoteException("Unable to get local resources when Container "
                                                     + containerID + " is at " + container.GetContainerState());
                }
                string user = container.GetUser();
                // /////////////////////////// Variable expansion
                // Before the container script gets written out.
                IList <string> newCmds  = new AList <string>(command.Count);
                string         appIdStr = app.GetAppId().ToString();
                string         relativeContainerLogDir = Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Launcher.ContainerLaunch
                                                         .GetRelativeContainerLogDir(appIdStr, containerIdStr);
                Path containerLogDir = dirsHandler.GetLogPathForWrite(relativeContainerLogDir, false
                                                                      );
                foreach (string str in command)
                {
                    // TODO: Should we instead work via symlinks without this grammar?
                    newCmds.AddItem(ExpandEnvironment(str, containerLogDir));
                }
                launchContext.SetCommands(newCmds);
                IDictionary <string, string> environment = launchContext.GetEnvironment();
                // Make a copy of env to iterate & do variable expansion
                foreach (KeyValuePair <string, string> entry in environment)
                {
                    string value = entry.Value;
                    value = ExpandEnvironment(value, containerLogDir);
                    entry.SetValue(value);
                }
                // /////////////////////////// End of variable expansion
                FileContext lfs = FileContext.GetLocalFSFileContext();
                Path        nmPrivateContainerScriptPath = dirsHandler.GetLocalPathForWrite(GetContainerPrivateDir
                                                                                                (appIdStr, containerIdStr) + Path.Separator + ContainerScript);
                Path nmPrivateTokensPath = dirsHandler.GetLocalPathForWrite(GetContainerPrivateDir
                                                                                (appIdStr, containerIdStr) + Path.Separator + string.Format(ContainerLocalizer.TokenFileNameFmt
                                                                                                                                            , containerIdStr));
                Path nmPrivateClasspathJarDir = dirsHandler.GetLocalPathForWrite(GetContainerPrivateDir
                                                                                     (appIdStr, containerIdStr));
                DataOutputStream containerScriptOutStream = null;
                DataOutputStream tokensOutStream          = null;
                // Select the working directory for the container
                Path containerWorkDir = dirsHandler.GetLocalPathForWrite(ContainerLocalizer.Usercache
                                                                         + Path.Separator + user + Path.Separator + ContainerLocalizer.Appcache + Path.Separator
                                                                         + appIdStr + Path.Separator + containerIdStr, LocalDirAllocator.SizeUnknown, false
                                                                         );
                string pidFileSubpath = GetPidFileSubpath(appIdStr, containerIdStr);
                // pid file should be in nm private dir so that it is not
                // accessible by users
                pidFilePath = dirsHandler.GetLocalPathForWrite(pidFileSubpath);
                IList <string> localDirs        = dirsHandler.GetLocalDirs();
                IList <string> logDirs          = dirsHandler.GetLogDirs();
                IList <string> containerLogDirs = new AList <string>();
                foreach (string logDir in logDirs)
                {
                    containerLogDirs.AddItem(logDir + Path.Separator + relativeContainerLogDir);
                }
                if (!dirsHandler.AreDisksHealthy())
                {
                    ret = ContainerExitStatus.DisksFailed;
                    throw new IOException("Most of the disks failed. " + dirsHandler.GetDisksHealthReport
                                              (false));
                }
                try
                {
                    // /////////// Write out the container-script in the nmPrivate space.
                    IList <Path> appDirs = new AList <Path>(localDirs.Count);
                    foreach (string localDir in localDirs)
                    {
                        Path usersdir = new Path(localDir, ContainerLocalizer.Usercache);
                        Path userdir  = new Path(usersdir, user);
                        Path appsdir  = new Path(userdir, ContainerLocalizer.Appcache);
                        appDirs.AddItem(new Path(appsdir, appIdStr));
                    }
                    containerScriptOutStream = lfs.Create(nmPrivateContainerScriptPath, EnumSet.Of(CreateFlag
                                                                                                   .Create, CreateFlag.Overwrite));
                    // Set the token location too.
                    environment[ApplicationConstants.ContainerTokenFileEnvName] = new Path(containerWorkDir
                                                                                           , FinalContainerTokensFile).ToUri().GetPath();
                    // Sanitize the container's environment
                    SanitizeEnv(environment, containerWorkDir, appDirs, containerLogDirs, localResources
                                , nmPrivateClasspathJarDir);
                    // Write out the environment
                    exec.WriteLaunchEnv(containerScriptOutStream, environment, localResources, launchContext
                                        .GetCommands());
                    // /////////// End of writing out container-script
                    // /////////// Write out the container-tokens in the nmPrivate space.
                    tokensOutStream = lfs.Create(nmPrivateTokensPath, EnumSet.Of(CreateFlag.Create, CreateFlag
                                                                                 .Overwrite));
                    Credentials creds = container.GetCredentials();
                    creds.WriteTokenStorageToStream(tokensOutStream);
                }
                finally
                {
                    // /////////// End of writing out container-tokens
                    IOUtils.Cleanup(Log, containerScriptOutStream, tokensOutStream);
                }
                // LaunchContainer is a blocking call. We are here almost means the
                // container is launched, so send out the event.
                dispatcher.GetEventHandler().Handle(new ContainerEvent(containerID, ContainerEventType
                                                                       .ContainerLaunched));
                context.GetNMStateStore().StoreContainerLaunched(containerID);
                // Check if the container is signalled to be killed.
                if (!shouldLaunchContainer.CompareAndSet(false, true))
                {
                    Log.Info("Container " + containerIdStr + " not launched as " + "cleanup already called"
                             );
                    ret = ContainerExecutor.ExitCode.Terminated.GetExitCode();
                }
                else
                {
                    exec.ActivateContainer(containerID, pidFilePath);
                    ret = exec.LaunchContainer(container, nmPrivateContainerScriptPath, nmPrivateTokensPath
                                               , user, appIdStr, containerWorkDir, localDirs, logDirs);
                }
            }
            catch (Exception e)
            {
                Log.Warn("Failed to launch container.", e);
                dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType
                                                                           .ContainerExitedWithFailure, ret, e.Message));
                return(ret);
            }
            finally
            {
                completed.Set(true);
                exec.DeactivateContainer(containerID);
                try
                {
                    context.GetNMStateStore().StoreContainerCompleted(containerID, ret);
                }
                catch (IOException)
                {
                    Log.Error("Unable to set exit code for container " + containerID);
                }
            }
            if (Log.IsDebugEnabled())
            {
                Log.Debug("Container " + containerIdStr + " completed with exit code " + ret);
            }
            if (ret == ContainerExecutor.ExitCode.ForceKilled.GetExitCode() || ret == ContainerExecutor.ExitCode
                .Terminated.GetExitCode())
            {
                // If the process was killed, Send container_cleanedup_after_kill and
                // just break out of this method.
                dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType
                                                                           .ContainerKilledOnRequest, ret, "Container exited with a non-zero exit code " +
                                                                           ret));
                return(ret);
            }
            if (ret != 0)
            {
                Log.Warn("Container exited with a non-zero exit code " + ret);
                this.dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType
                                                                                .ContainerExitedWithFailure, ret, "Container exited with a non-zero exit code "
                                                                                + ret));
                return(ret);
            }
            Log.Info("Container " + containerIdStr + " succeeded ");
            dispatcher.GetEventHandler().Handle(new ContainerEvent(containerID, ContainerEventType
                                                                   .ContainerExitedWithSuccess));
            return(0);
        }
Beispiel #14
0
        /// <summary>Common routine to do position read while open the file for write.</summary>
        /// <remarks>
        /// Common routine to do position read while open the file for write.
        /// After each iteration of write, do a read of the file from begin to end.
        /// Return 0 on success, else number of failure.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        private int TestWriteAndRead(string fname, int loopN, int chunkSize, long readBeginPosition
                                     )
        {
            int  countOfFailures    = 0;
            long byteVisibleToRead  = 0;
            FSDataOutputStream @out = null;

            byte[] outBuffer = new byte[BufferSize];
            byte[] inBuffer  = new byte[BufferSize];
            for (int i = 0; i < BufferSize; i++)
            {
                outBuffer[i] = unchecked ((byte)(i & unchecked ((int)(0x00ff))));
            }
            try
            {
                Path path = GetFullyQualifiedPath(fname);
                long fileLengthBeforeOpen = 0;
                if (IfExists(path))
                {
                    if (truncateOption)
                    {
                        @out = useFCOption ? mfc.Create(path, EnumSet.Of(CreateFlag.Overwrite)) : mfs.Create
                                   (path, truncateOption);
                        Log.Info("File already exists. File open with Truncate mode: " + path);
                    }
                    else
                    {
                        @out = useFCOption ? mfc.Create(path, EnumSet.Of(CreateFlag.Append)) : mfs.Append
                                   (path);
                        fileLengthBeforeOpen = GetFileLengthFromNN(path);
                        Log.Info("File already exists of size " + fileLengthBeforeOpen + " File open for Append mode: "
                                 + path);
                    }
                }
                else
                {
                    @out = useFCOption ? mfc.Create(path, EnumSet.Of(CreateFlag.Create)) : mfs.Create
                               (path);
                }
                long totalByteWritten = fileLengthBeforeOpen;
                long totalByteVisible = fileLengthBeforeOpen;
                long totalByteWrittenButNotVisible = 0;
                bool toFlush;
                for (int i_1 = 0; i_1 < loopN; i_1++)
                {
                    toFlush = (i_1 % 2) == 0;
                    WriteData(@out, outBuffer, chunkSize);
                    totalByteWritten += chunkSize;
                    if (toFlush)
                    {
                        @out.Hflush();
                        totalByteVisible += chunkSize + totalByteWrittenButNotVisible;
                        totalByteWrittenButNotVisible = 0;
                    }
                    else
                    {
                        totalByteWrittenButNotVisible += chunkSize;
                    }
                    if (verboseOption)
                    {
                        Log.Info("TestReadWrite - Written " + chunkSize + ". Total written = " + totalByteWritten
                                 + ". TotalByteVisible = " + totalByteVisible + " to file " + fname);
                    }
                    byteVisibleToRead = ReadData(fname, inBuffer, totalByteVisible, readBeginPosition
                                                 );
                    string readmsg = "Written=" + totalByteWritten + " ; Expected Visible=" + totalByteVisible
                                     + " ; Got Visible=" + byteVisibleToRead + " of file " + fname;
                    if (byteVisibleToRead >= totalByteVisible && byteVisibleToRead <= totalByteWritten)
                    {
                        readmsg = "pass: reader sees expected number of visible byte. " + readmsg + " [pass]";
                    }
                    else
                    {
                        countOfFailures++;
                        readmsg = "fail: reader see different number of visible byte. " + readmsg + " [fail]";
                        throw new IOException(readmsg);
                    }
                    Log.Info(readmsg);
                }
                // test the automatic flush after close
                WriteData(@out, outBuffer, chunkSize);
                totalByteWritten += chunkSize;
                totalByteVisible += chunkSize + totalByteWrittenButNotVisible;
                totalByteWrittenButNotVisible += 0;
                @out.Close();
                byteVisibleToRead = ReadData(fname, inBuffer, totalByteVisible, readBeginPosition
                                             );
                string readmsg2 = "Written=" + totalByteWritten + " ; Expected Visible=" + totalByteVisible
                                  + " ; Got Visible=" + byteVisibleToRead + " of file " + fname;
                string readmsg_1;
                if (byteVisibleToRead >= totalByteVisible && byteVisibleToRead <= totalByteWritten)
                {
                    readmsg_1 = "pass: reader sees expected number of visible byte on close. " + readmsg2
                                + " [pass]";
                }
                else
                {
                    countOfFailures++;
                    readmsg_1 = "fail: reader sees different number of visible byte on close. " + readmsg2
                                + " [fail]";
                    Log.Info(readmsg_1);
                    throw new IOException(readmsg_1);
                }
                // now check if NN got the same length
                long lenFromFc = GetFileLengthFromNN(path);
                if (lenFromFc != byteVisibleToRead)
                {
                    readmsg_1 = "fail: reader sees different number of visible byte from NN " + readmsg2
                                + " [fail]";
                    throw new IOException(readmsg_1);
                }
            }
            catch (IOException e)
            {
                throw new IOException("##### Caught Exception in testAppendWriteAndRead. Close file. "
                                      + "Total Byte Read so far = " + byteVisibleToRead, e);
            }
            finally
            {
                if (@out != null)
                {
                    @out.Close();
                }
            }
            return(-countOfFailures);
        }
        //[Authorize]
        public async Task <HttpResponseMessage> UploadChunkAsync()
        {
            SqlConnection  myConnection = new SqlConnection();
            SqlTransaction myTransaction;

            using (myConnection)
            {
                myConnection.ConnectionString = ConnectionContext.GetConnectionString();
                myConnection.Open();

                // TRANSACTION - in case of SQL error or File write to disk error - rollback will be awailable
                myTransaction = myConnection.BeginTransaction("UploadFileTransaction");

                try
                {
                    SetUserDetails();
                    HttpResponseMessage response;
                    String responseMsg;

                    // check if such hash has record in DB.
                    CFile newFile = _fileContext.GetByHash(fileInfo.Hash, myConnection, myTransaction);

                    // if such hash and exist in DB and user names match
                    if (!newFile.Guid.Equals(Guid.Empty) && newFile.UserId.Equals(user.Guid))
                    {
                        response = Request.CreateResponse(HttpStatusCode.BadRequest);
                        response.ReasonPhrase = $"Another file {newFile.Name} with same hash than{fileInfo.Name} already exists on Media Server!";
                        return(response);
                    }

                    // check if such file name has record in DB.
                    newFile = _fileContext.GetByFileName(fileInfo.Name, user.Guid, myConnection, myTransaction);
                    // if file with such name already exists in DB
                    if (!newFile.Guid.Equals(Guid.Empty))
                    {
                        // file exits, it has hash (so it is 100% loaded), but hash is different, which means this is the other file!
                        if (newFile.Hash != null)
                        {
                            response = Request.CreateResponse(HttpStatusCode.BadRequest);
                            response.ReasonPhrase = $"Another file {newFile.Name} with same hash than{fileInfo.Name} already exists on Media Server!";
                            return(response);
                        }
                        else
                        {
                            // file exists, but it has no hash, so it hasn't been 100% loaded - continue loading
                        }
                    }
                    // fileName not found in DB - create new file record
                    else
                    {
                        // add record to DB with file info
                        newFile = new CFile(fileInfo, user, userFolder);

                        // create record in DB about new file
                        Int32 added = _fileContext.Create(newFile, myConnection, myTransaction);

                        if (added == 0)
                        {
                            response = Request.CreateResponse(HttpStatusCode.BadRequest, $"Cannot add file to DB!");
                            return(response);
                        }

                        // SQL generates primary field values, so get created file back with proper Guid
                        newFile = _fileContext.GetByFileName(newFile.Name, user.Guid, myConnection, myTransaction);

                        // add file to all playlists specified by user. Default playlist is 'ON' if no other playlists are specified.
                        if (fileInfo.playlists.Count == 0)
                        {
                            CPlaylist playlist = _playlistContext.GetByName("default", user.Guid);
                            _fileContext.AddToPlaylist(newFile.Guid, playlist.Guid, myConnection, myTransaction);
                        }
                        else
                        {
                            // add files to other playlists specified by user
                            foreach (CPlaylistInfo playlistInfo in fileInfo.playlists)
                            {
                                CPlaylist playlist = new CPlaylist(playlistInfo);
                                _fileContext.AddToPlaylist(newFile.Guid, playlist.Guid, myConnection, myTransaction);
                            }
                        }
                    }

                    // After all checks on existing files, continue loading given chunk of byte data
                    //todo: upload directly to memory stream or to file stream?
                    MemoryStream ms = new MemoryStream(new byte[chunkSize], true);
                    using (ms)
                    {
                        await Request.Content.CopyToAsync(ms);

                        // check if all bytes from the chunk have been loaded (no disconnect or program crash happened during load)
                        if (ms.Length == chunkSize)
                        {
                            using (FileStream fs = new FileStream
                                                       (newFile.Path + newFile.Name, FileMode.Append, FileAccess.Write, FileShare.None, chunkSize, useAsync: true))
                            {
                                ms.WriteTo(fs);
                            }
                        }
                        else
                        {
                            // couldn't upload file chunk - so rollback record from DB
                            myTransaction.Rollback();

                            response = Request.CreateResponse(HttpStatusCode.OK, $"Chunk has not been uploaded correctly!");
                            return(response);
                        }
                    }

                    responseMsg = $"Chunk for file:{newFile.Name} successfully uploaded!";

                    // calculate Hash only if file has been loaded 100%
                    if (isLastChunk)
                    {
                        // calculate Hash only if file has been loaded 100%
                        await Task.Factory.StartNew(() => HashFile(newFile)).ContinueWith(delegate
                        {
                            // calculate Thumbnail for video only if file has been loaded 100%
                            CreateThumbnail(newFile);
                        });

                        responseMsg = $"File  successfully uploaded!";
                    }

                    // update file data in DB: hash, fileSize, ...
                    newFile.Size += chunkSize;
                    _fileContext.Update(newFile, myConnection, myTransaction);

                    // if all went OK, commit records to DB
                    myTransaction.Commit();

                    // return response if success
                    response = Request.CreateResponse(HttpStatusCode.OK, responseMsg);
                    return(response);
                }
                catch (Exception e)
                {
                    myTransaction.Rollback();
                    HttpContext.Current.Response.StatusCode = (Int32)HttpStatusCode.BadRequest;
                    throw new FileUploadException(e.Message, e);
                }
            }
        }