Esempio n. 1
0
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        protected internal virtual void FetchData()
        {
            reqAppStates = EnumSet.NoneOf <YarnApplicationState>();
            string reqStateString = $(YarnWebParams.AppState);

            if (reqStateString != null && !reqStateString.IsEmpty())
            {
                string[] appStateStrings = reqStateString.Split(",");
                foreach (string stateString in appStateStrings)
                {
                    reqAppStates.AddItem(YarnApplicationState.ValueOf(stateString.Trim()));
                }
            }
            callerUGI = GetCallerUGI();
            GetApplicationsRequest request = GetApplicationsRequest.NewInstance(reqAppStates);
            string appsNumStr = $(YarnWebParams.AppsNum);

            if (appsNumStr != null && !appsNumStr.IsEmpty())
            {
                long appsNum = long.Parse(appsNumStr);
                request.SetLimit(appsNum);
            }
            if (callerUGI == null)
            {
                appReports = appBaseProt.GetApplications(request).GetApplicationList();
            }
            else
            {
                appReports = callerUGI.DoAs(new _PrivilegedExceptionAction_87(this, request));
            }
        }
Esempio n. 2
0
        /// <exception cref="System.IO.IOException"/>
        protected internal void MakeTestFile(Path path, long length, bool isLazyPersist)
        {
            EnumSet <CreateFlag> createFlags = EnumSet.Of(CreateFlag.Create);

            if (isLazyPersist)
            {
                createFlags.AddItem(CreateFlag.LazyPersist);
            }
            FSDataOutputStream fos = null;

            try
            {
                fos = fs.Create(path, FsPermission.GetFileDefault(), createFlags, BufferLength, ReplFactor
                                , BlockSize, null);
                // Allocate a block.
                byte[] buffer = new byte[BufferLength];
                for (int bytesWritten = 0; bytesWritten < length;)
                {
                    fos.Write(buffer, 0, buffer.Length);
                    bytesWritten += buffer.Length;
                }
                if (length > 0)
                {
                    fos.Hsync();
                }
            }
            finally
            {
                IOUtils.CloseQuietly(fos);
            }
        }
Esempio n. 3
0
        /// <summary>Calculates mask entries required for the ACL.</summary>
        /// <remarks>
        /// Calculates mask entries required for the ACL.  Mask calculation is performed
        /// separately for each scope: access and default.  This method is responsible
        /// for handling the following cases of mask calculation:
        /// 1. Throws an exception if the caller attempts to remove the mask entry of an
        /// existing ACL that requires it.  If the ACL has any named entries, then a
        /// mask entry is required.
        /// 2. If the caller supplied a mask in the ACL spec, use it.
        /// 3. If the caller did not supply a mask, but there are ACL entry changes in
        /// this scope, then automatically calculate a new mask.  The permissions of
        /// the new mask are the union of the permissions on the group entry and all
        /// named entries.
        /// </remarks>
        /// <param name="aclBuilder">ArrayList<AclEntry> containing entries to build</param>
        /// <param name="providedMask">
        /// EnumMap<AclEntryScope, AclEntry> mapping each scope to
        /// the mask entry that was provided for that scope (if provided)
        /// </param>
        /// <param name="maskDirty">
        /// EnumSet<AclEntryScope> which contains a scope if the mask
        /// entry is dirty (added or deleted) in that scope
        /// </param>
        /// <param name="scopeDirty">
        /// EnumSet<AclEntryScope> which contains a scope if any entry
        /// is dirty (added or deleted) in that scope
        /// </param>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.AclException">if validation fails
        ///     </exception>
        private static void CalculateMasks(IList <AclEntry> aclBuilder, EnumMap <AclEntryScope
                                                                                 , AclEntry> providedMask, EnumSet <AclEntryScope> maskDirty, EnumSet <AclEntryScope
                                                                                                                                                       > scopeDirty)
        {
            EnumSet <AclEntryScope>           scopeFound = EnumSet.NoneOf <AclEntryScope>();
            EnumMap <AclEntryScope, FsAction> unionPerms = Maps.NewEnumMap <AclEntryScope>();
            EnumSet <AclEntryScope>           maskNeeded = EnumSet.NoneOf <AclEntryScope>();

            // Determine which scopes are present, which scopes need a mask, and the
            // union of group class permissions in each scope.
            foreach (AclEntry entry in aclBuilder)
            {
                scopeFound.AddItem(entry.GetScope());
                if (entry.GetType() == AclEntryType.Group || entry.GetName() != null)
                {
                    FsAction scopeUnionPerms = Objects.FirstNonNull(unionPerms[entry.GetScope()], FsAction
                                                                    .None);
                    unionPerms[entry.GetScope()] = scopeUnionPerms.Or(entry.GetPermission());
                }
                if (entry.GetName() != null)
                {
                    maskNeeded.AddItem(entry.GetScope());
                }
            }
            // Add mask entry if needed in each scope.
            foreach (AclEntryScope scope in scopeFound)
            {
                if (!providedMask.Contains(scope) && maskNeeded.Contains(scope) && maskDirty.Contains
                        (scope))
                {
                    // Caller explicitly removed mask entry, but it's required.
                    throw new AclException("Invalid ACL: mask is required and cannot be deleted.");
                }
                else
                {
                    if (providedMask.Contains(scope) && (!scopeDirty.Contains(scope) || maskDirty.Contains
                                                             (scope)))
                    {
                        // Caller explicitly provided new mask, or we are preserving the existing
                        // mask in an unchanged scope.
                        aclBuilder.AddItem(providedMask[scope]);
                    }
                    else
                    {
                        if (maskNeeded.Contains(scope) || providedMask.Contains(scope))
                        {
                            // Otherwise, if there are maskable entries present, or the ACL
                            // previously had a mask, then recalculate a mask automatically.
                            aclBuilder.AddItem(new AclEntry.Builder().SetScope(scope).SetType(AclEntryType.Mask
                                                                                              ).SetPermission(unionPerms[scope]).Build());
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public virtual void Deserialize(XDR xdr)
        {
            if (xdr.ReadBoolean())
            {
                mode = xdr.ReadInt();
                updateFields.AddItem(SetAttr3.SetAttrField.Mode);
            }
            if (xdr.ReadBoolean())
            {
                uid = xdr.ReadInt();
                updateFields.AddItem(SetAttr3.SetAttrField.Uid);
            }
            if (xdr.ReadBoolean())
            {
                gid = xdr.ReadInt();
                updateFields.AddItem(SetAttr3.SetAttrField.Gid);
            }
            if (xdr.ReadBoolean())
            {
                size = xdr.ReadHyper();
                updateFields.AddItem(SetAttr3.SetAttrField.Size);
            }
            int timeSetHow = xdr.ReadInt();

            if (timeSetHow == TimeSetToClientTime)
            {
                atime = NfsTime.Deserialize(xdr);
                updateFields.AddItem(SetAttr3.SetAttrField.Atime);
            }
            else
            {
                if (timeSetHow == TimeSetToServerTime)
                {
                    atime = new NfsTime(Runtime.CurrentTimeMillis());
                    updateFields.AddItem(SetAttr3.SetAttrField.Atime);
                }
            }
            timeSetHow = xdr.ReadInt();
            if (timeSetHow == TimeSetToClientTime)
            {
                mtime = NfsTime.Deserialize(xdr);
                updateFields.AddItem(SetAttr3.SetAttrField.Mtime);
            }
            else
            {
                if (timeSetHow == TimeSetToServerTime)
                {
                    mtime = new NfsTime(Runtime.CurrentTimeMillis());
                    updateFields.AddItem(SetAttr3.SetAttrField.Mtime);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Lists the applications matching the given application Types And application
        /// States present in the Resource Manager
        /// </summary>
        /// <param name="appTypes"/>
        /// <param name="appStates"/>
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        /// <exception cref="System.IO.IOException"/>
        private void ListApplications(ICollection <string> appTypes, EnumSet <YarnApplicationState
                                                                              > appStates)
        {
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(sysout, Sharpen.Extensions.GetEncoding
                                                                            ("UTF-8")));

            if (allAppStates)
            {
                foreach (YarnApplicationState appState in YarnApplicationState.Values())
                {
                    appStates.AddItem(appState);
                }
            }
            else
            {
                if (appStates.IsEmpty())
                {
                    appStates.AddItem(YarnApplicationState.Running);
                    appStates.AddItem(YarnApplicationState.Accepted);
                    appStates.AddItem(YarnApplicationState.Submitted);
                }
            }
            IList <ApplicationReport> appsReport = client.GetApplications(appTypes, appStates);

            writer.WriteLine("Total number of applications (application-types: " + appTypes +
                             " and states: " + appStates + ")" + ":" + appsReport.Count);
            writer.Printf(ApplicationsPattern, "Application-Id", "Application-Name", "Application-Type"
                          , "User", "Queue", "State", "Final-State", "Progress", "Tracking-URL");
            foreach (ApplicationReport appReport in appsReport)
            {
                DecimalFormat formatter = new DecimalFormat("###.##%");
                string        progress  = formatter.Format(appReport.GetProgress());
                writer.Printf(ApplicationsPattern, appReport.GetApplicationId(), appReport.GetName
                                  (), appReport.GetApplicationType(), appReport.GetUser(), appReport.GetQueue(), appReport
                              .GetYarnApplicationState(), appReport.GetFinalApplicationStatus(), progress, appReport
                              .GetOriginalTrackingUrl());
            }
            writer.Flush();
        }
Esempio n. 6
0
        /// <exception cref="System.Exception"/>
        protected internal override EnumSet <E> Parse(string str)
        {
            EnumSet <E> set = EnumSet.NoneOf(klass);

            if (!str.IsEmpty())
            {
                foreach (string sub in str.Split(","))
                {
                    set.AddItem(Enum.ValueOf(klass, StringUtils.ToUpperCase(sub.Trim())));
                }
            }
            return(set);
        }
Esempio n. 7
0
 public static EnumSet <T> Add <T>(EnumSet <T> set, string s)
     where T : Enum <T>
 {
     System.Type c = typeof(T);
     if (null != fullmap[c] && fullmap[c][s] != null)
     {
         if (null == set)
         {
             set = EnumSet.NoneOf(c);
         }
         set.AddItem((T)fullmap[c][s]);
     }
     return(set);
 }
Esempio n. 8
0
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        /// <exception cref="System.IO.IOException"/>
        public override IList <NodeReport> GetNodeReports(params NodeState[] states)
        {
            EnumSet <NodeState> statesSet = (states.Length == 0) ? EnumSet.AllOf <NodeState>() :
                                            EnumSet.NoneOf <NodeState>();

            foreach (NodeState state in states)
            {
                statesSet.AddItem(state);
            }
            GetClusterNodesRequest  request  = GetClusterNodesRequest.NewInstance(statesSet);
            GetClusterNodesResponse response = rmClient.GetClusterNodes(request);

            return(response.GetNodeReports());
        }
Esempio n. 9
0
            /// <summary>The string contains a comma separated values.</summary>
            internal sealed override EnumSet <E> Parse(string str)
            {
                EnumSet <E> set = EnumSet.NoneOf(enumClass);

                if (!str.IsEmpty())
                {
                    for (int i; j >= 0;)
                    {
                        i = j > 0 ? j + 1 : 0;
                        j = str.IndexOf(',', i);
                        string sub = j >= 0 ? Sharpen.Runtime.Substring(str, i, j) : Sharpen.Runtime.Substring
                                         (str, i);
                        set.AddItem(Enum.ValueOf(enumClass, StringUtils.ToUpperCase(sub.Trim())));
                    }
                }
                return(set);
            }
Esempio n. 10
0
        /// <summary>Completely replaces the ACL with the entries of the ACL spec.</summary>
        /// <remarks>
        /// Completely replaces the ACL with the entries of the ACL spec.  If
        /// necessary, recalculates the mask entries.  If necessary, default entries
        /// are inferred by copying the permissions of the corresponding access
        /// entries.  Replacement occurs separately for each of the access ACL and the
        /// default ACL.  If the ACL spec contains only access entries, then the
        /// existing default entries are retained.  If the ACL spec contains only
        /// default entries, then the existing access entries are retained.  If the ACL
        /// spec contains both access and default entries, then both are replaced.
        /// </remarks>
        /// <param name="existingAcl">List<AclEntry> existing ACL</param>
        /// <param name="inAclSpec">List<AclEntry> ACL spec containing replacement entries</param>
        /// <returns>List<AclEntry> new ACL</returns>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.AclException">if validation fails
        ///     </exception>
        public static IList <AclEntry> ReplaceAclEntries(IList <AclEntry> existingAcl, IList
                                                         <AclEntry> inAclSpec)
        {
            AclTransformation.ValidatedAclSpec aclSpec = new AclTransformation.ValidatedAclSpec
                                                             (inAclSpec);
            AList <AclEntry> aclBuilder = Lists.NewArrayListWithCapacity(MaxEntries);
            // Replacement is done separately for each scope: access and default.
            EnumMap <AclEntryScope, AclEntry> providedMask = Maps.NewEnumMap <AclEntryScope>();
            EnumSet <AclEntryScope>           maskDirty    = EnumSet.NoneOf <AclEntryScope>();
            EnumSet <AclEntryScope>           scopeDirty   = EnumSet.NoneOf <AclEntryScope>();

            foreach (AclEntry aclSpecEntry in aclSpec)
            {
                scopeDirty.AddItem(aclSpecEntry.GetScope());
                if (aclSpecEntry.GetType() == AclEntryType.Mask)
                {
                    providedMask[aclSpecEntry.GetScope()] = aclSpecEntry;
                    maskDirty.AddItem(aclSpecEntry.GetScope());
                }
                else
                {
                    aclBuilder.AddItem(aclSpecEntry);
                }
            }
            // Copy existing entries if the scope was not replaced.
            foreach (AclEntry existingEntry in existingAcl)
            {
                if (!scopeDirty.Contains(existingEntry.GetScope()))
                {
                    if (existingEntry.GetType() == AclEntryType.Mask)
                    {
                        providedMask[existingEntry.GetScope()] = existingEntry;
                    }
                    else
                    {
                        aclBuilder.AddItem(existingEntry);
                    }
                }
            }
            CopyDefaultsIfNeeded(aclBuilder);
            CalculateMasks(aclBuilder, providedMask, maskDirty, scopeDirty);
            return(BuildAndValidateAcl(aclBuilder));
        }
        public override void SetApplicationStates(ICollection <string> applicationStates)
        {
            EnumSet <YarnApplicationState> appStates = null;

            foreach (YarnApplicationState state in YarnApplicationState.Values())
            {
                if (applicationStates.Contains(StringUtils.ToLowerCase(state.ToString())))
                {
                    if (appStates == null)
                    {
                        appStates = EnumSet.Of(state);
                    }
                    else
                    {
                        appStates.AddItem(state);
                    }
                }
            }
            SetApplicationStates(appStates);
        }
Esempio n. 12
0
        /// <summary>
        /// Filters (discards) any existing ACL entries that have the same scope, type
        /// and name of any entry in the ACL spec.
        /// </summary>
        /// <remarks>
        /// Filters (discards) any existing ACL entries that have the same scope, type
        /// and name of any entry in the ACL spec.  If necessary, recalculates the mask
        /// entries.  If necessary, default entries may be inferred by copying the
        /// permissions of the corresponding access entries.  It is invalid to request
        /// removal of the mask entry from an ACL that would otherwise require a mask
        /// entry, due to existing named entries or an unnamed group entry.
        /// </remarks>
        /// <param name="existingAcl">List<AclEntry> existing ACL</param>
        /// <param name="inAclSpec">List<AclEntry> ACL spec describing entries to filter</param>
        /// <returns>List<AclEntry> new ACL</returns>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.AclException">if validation fails
        ///     </exception>
        public static IList <AclEntry> FilterAclEntriesByAclSpec(IList <AclEntry> existingAcl
                                                                 , IList <AclEntry> inAclSpec)
        {
            AclTransformation.ValidatedAclSpec aclSpec = new AclTransformation.ValidatedAclSpec
                                                             (inAclSpec);
            AList <AclEntry> aclBuilder = Lists.NewArrayListWithCapacity(MaxEntries);
            EnumMap <AclEntryScope, AclEntry> providedMask = Maps.NewEnumMap <AclEntryScope>();
            EnumSet <AclEntryScope>           maskDirty    = EnumSet.NoneOf <AclEntryScope>();
            EnumSet <AclEntryScope>           scopeDirty   = EnumSet.NoneOf <AclEntryScope>();

            foreach (AclEntry existingEntry in existingAcl)
            {
                if (aclSpec.ContainsKey(existingEntry))
                {
                    scopeDirty.AddItem(existingEntry.GetScope());
                    if (existingEntry.GetType() == AclEntryType.Mask)
                    {
                        maskDirty.AddItem(existingEntry.GetScope());
                    }
                }
                else
                {
                    if (existingEntry.GetType() == AclEntryType.Mask)
                    {
                        providedMask[existingEntry.GetScope()] = existingEntry;
                    }
                    else
                    {
                        aclBuilder.AddItem(existingEntry);
                    }
                }
            }
            CopyDefaultsIfNeeded(aclBuilder);
            CalculateMasks(aclBuilder, providedMask, maskDirty, scopeDirty);
            return(BuildAndValidateAcl(aclBuilder));
        }
Esempio n. 13
0
        /// <summary>Merges the entries of the ACL spec into the existing ACL.</summary>
        /// <remarks>
        /// Merges the entries of the ACL spec into the existing ACL.  If necessary,
        /// recalculates the mask entries.  If necessary, default entries may be
        /// inferred by copying the permissions of the corresponding access entries.
        /// </remarks>
        /// <param name="existingAcl">List<AclEntry> existing ACL</param>
        /// <param name="inAclSpec">List<AclEntry> ACL spec containing entries to merge</param>
        /// <returns>List<AclEntry> new ACL</returns>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.AclException">if validation fails
        ///     </exception>
        public static IList <AclEntry> MergeAclEntries(IList <AclEntry> existingAcl, IList <
                                                           AclEntry> inAclSpec)
        {
            AclTransformation.ValidatedAclSpec aclSpec = new AclTransformation.ValidatedAclSpec
                                                             (inAclSpec);
            AList <AclEntry> aclBuilder                    = Lists.NewArrayListWithCapacity(MaxEntries);
            IList <AclEntry> foundAclSpecEntries           = Lists.NewArrayListWithCapacity(MaxEntries);
            EnumMap <AclEntryScope, AclEntry> providedMask = Maps.NewEnumMap <AclEntryScope>();
            EnumSet <AclEntryScope>           maskDirty    = EnumSet.NoneOf <AclEntryScope>();
            EnumSet <AclEntryScope>           scopeDirty   = EnumSet.NoneOf <AclEntryScope>();

            foreach (AclEntry existingEntry in existingAcl)
            {
                AclEntry aclSpecEntry = aclSpec.FindByKey(existingEntry);
                if (aclSpecEntry != null)
                {
                    foundAclSpecEntries.AddItem(aclSpecEntry);
                    scopeDirty.AddItem(aclSpecEntry.GetScope());
                    if (aclSpecEntry.GetType() == AclEntryType.Mask)
                    {
                        providedMask[aclSpecEntry.GetScope()] = aclSpecEntry;
                        maskDirty.AddItem(aclSpecEntry.GetScope());
                    }
                    else
                    {
                        aclBuilder.AddItem(aclSpecEntry);
                    }
                }
                else
                {
                    if (existingEntry.GetType() == AclEntryType.Mask)
                    {
                        providedMask[existingEntry.GetScope()] = existingEntry;
                    }
                    else
                    {
                        aclBuilder.AddItem(existingEntry);
                    }
                }
            }
            // ACL spec entries that were not replacements are new additions.
            foreach (AclEntry newEntry in aclSpec)
            {
                if (Sharpen.Collections.BinarySearch(foundAclSpecEntries, newEntry, AclEntryComparator
                                                     ) < 0)
                {
                    scopeDirty.AddItem(newEntry.GetScope());
                    if (newEntry.GetType() == AclEntryType.Mask)
                    {
                        providedMask[newEntry.GetScope()] = newEntry;
                        maskDirty.AddItem(newEntry.GetScope());
                    }
                    else
                    {
                        aclBuilder.AddItem(newEntry);
                    }
                }
            }
            CopyDefaultsIfNeeded(aclBuilder);
            CalculateMasks(aclBuilder, providedMask, maskDirty, scopeDirty);
            return(BuildAndValidateAcl(aclBuilder));
        }
Esempio n. 14
0
            /// <exception cref="System.IO.IOException"/>
            public virtual int Run(Configuration conf, IList <string> args)
            {
                CacheDirectiveInfo.Builder builder = new CacheDirectiveInfo.Builder();
                string path = StringUtils.PopOptionWithArgument("-path", args);

                if (path == null)
                {
                    System.Console.Error.WriteLine("You must specify a path with -path.");
                    return(1);
                }
                builder.SetPath(new Path(path));
                string poolName = StringUtils.PopOptionWithArgument("-pool", args);

                if (poolName == null)
                {
                    System.Console.Error.WriteLine("You must specify a pool name with -pool.");
                    return(1);
                }
                builder.SetPool(poolName);
                bool   force             = StringUtils.PopOption("-force", args);
                string replicationString = StringUtils.PopOptionWithArgument("-replication", args
                                                                             );

                if (replicationString != null)
                {
                    short replication = short.ParseShort(replicationString);
                    builder.SetReplication(replication);
                }
                string ttlString = StringUtils.PopOptionWithArgument("-ttl", args);

                try
                {
                    CacheDirectiveInfo.Expiration ex = ParseExpirationString(ttlString);
                    if (ex != null)
                    {
                        builder.SetExpiration(ex);
                    }
                }
                catch (IOException e)
                {
                    System.Console.Error.WriteLine("Error while parsing ttl value: " + e.Message);
                    return(1);
                }
                if (!args.IsEmpty())
                {
                    System.Console.Error.WriteLine("Can't understand argument: " + args[0]);
                    return(1);
                }
                DistributedFileSystem dfs       = AdminHelper.GetDFS(conf);
                CacheDirectiveInfo    directive = builder.Build();
                EnumSet <CacheFlag>   flags     = EnumSet.NoneOf <CacheFlag>();

                if (force)
                {
                    flags.AddItem(CacheFlag.Force);
                }
                try
                {
                    long id = dfs.AddCacheDirective(directive, flags);
                    System.Console.Out.WriteLine("Added cache directive " + id);
                }
                catch (IOException e)
                {
                    System.Console.Error.WriteLine(AdminHelper.PrettifyException(e));
                    return(2);
                }
                return(0);
            }
Esempio n. 15
0
        public void UpdateIndex()
        {
            Log.V(Log.TagView, "Re-indexing view: %s", name);
            System.Diagnostics.Debug.Assert((mapBlock != null));
            if (GetViewId() <= 0)
            {
                string msg = string.Format("getViewId() < 0");
                throw new CouchbaseLiteException(msg, new Status(Status.NotFound));
            }
            database.BeginTransaction();
            Status result = new Status(Status.InternalServerError);
            Cursor cursor = null;

            try
            {
                long lastSequence  = GetLastSequenceIndexed();
                long dbMaxSequence = database.GetLastSequenceNumber();
                if (lastSequence == dbMaxSequence)
                {
                    // nothing to do (eg,  kCBLStatusNotModified)
                    Log.V(Log.TagView, "lastSequence (%s) == dbMaxSequence (%s), nothing to do", lastSequence
                          , dbMaxSequence);
                    result.SetCode(Status.NotModified);
                    return;
                }
                // First remove obsolete emitted results from the 'maps' table:
                long sequence = lastSequence;
                if (lastSequence < 0)
                {
                    string msg = string.Format("lastSequence < 0 (%s)", lastSequence);
                    throw new CouchbaseLiteException(msg, new Status(Status.InternalServerError));
                }
                if (lastSequence == 0)
                {
                    // If the lastSequence has been reset to 0, make sure to remove
                    // any leftover rows:
                    string[] whereArgs = new string[] { Sharpen.Extensions.ToString(GetViewId()) };
                    database.GetDatabase().Delete("maps", "view_id=?", whereArgs);
                }
                else
                {
                    // Delete all obsolete map results (ones from since-replaced
                    // revisions):
                    string[] args = new string[] { Sharpen.Extensions.ToString(GetViewId()), System.Convert.ToString
                                                       (lastSequence), System.Convert.ToString(lastSequence) };
                    database.GetDatabase().ExecSQL("DELETE FROM maps WHERE view_id=? AND sequence IN ("
                                                   + "SELECT parent FROM revs WHERE sequence>? " + "AND parent>0 AND parent<=?)",
                                                   args);
                }
                int deleted = 0;
                cursor = database.GetDatabase().RawQuery("SELECT changes()", null);
                cursor.MoveToNext();
                deleted = cursor.GetInt(0);
                cursor.Close();
                // This is the emit() block, which gets called from within the
                // user-defined map() block
                // that's called down below.
                AbstractTouchMapEmitBlock emitBlock = new _AbstractTouchMapEmitBlock_428(this);
                //Log.v(Log.TAG_VIEW, "    emit(" + keyJson + ", "
                //        + valueJson + ")");
                // find a better way to propagate this back
                // Now scan every revision added since the last time the view was
                // indexed:
                string[] selectArgs = new string[] { System.Convert.ToString(lastSequence) };
                cursor = database.GetDatabase().RawQuery("SELECT revs.doc_id, sequence, docid, revid, json, no_attachments FROM revs, docs "
                                                         + "WHERE sequence>? AND current!=0 AND deleted=0 " + "AND revs.doc_id = docs.doc_id "
                                                         + "ORDER BY revs.doc_id, revid DESC", selectArgs);
                long lastDocID = 0;
                bool keepGoing = cursor.MoveToNext();
                while (keepGoing)
                {
                    long docID = cursor.GetLong(0);
                    if (docID != lastDocID)
                    {
                        // Only look at the first-iterated revision of any document,
                        // because this is the
                        // one with the highest revid, hence the "winning" revision
                        // of a conflict.
                        lastDocID = docID;
                        // Reconstitute the document as a dictionary:
                        sequence = cursor.GetLong(1);
                        string docId = cursor.GetString(2);
                        if (docId.StartsWith("_design/"))
                        {
                            // design docs don't get indexed!
                            keepGoing = cursor.MoveToNext();
                            continue;
                        }
                        string revId         = cursor.GetString(3);
                        byte[] json          = cursor.GetBlob(4);
                        bool   noAttachments = cursor.GetInt(5) > 0;
                        while ((keepGoing = cursor.MoveToNext()) && cursor.GetLong(0) == docID)
                        {
                        }
                        // Skip rows with the same doc_id -- these are losing conflicts.
                        if (lastSequence > 0)
                        {
                            // Find conflicts with documents from previous indexings.
                            string[] selectArgs2 = new string[] { System.Convert.ToString(docID), System.Convert.ToString
                                                                      (lastSequence) };
                            Cursor cursor2 = database.GetDatabase().RawQuery("SELECT revid, sequence FROM revs "
                                                                             + "WHERE doc_id=? AND sequence<=? AND current!=0 AND deleted=0 " + "ORDER BY revID DESC "
                                                                             + "LIMIT 1", selectArgs2);
                            if (cursor2.MoveToNext())
                            {
                                string oldRevId = cursor2.GetString(0);
                                // This is the revision that used to be the 'winner'.
                                // Remove its emitted rows:
                                long     oldSequence = cursor2.GetLong(1);
                                string[] args        = new string[] { Sharpen.Extensions.ToString(GetViewId()), System.Convert.ToString
                                                                          (oldSequence) };
                                database.GetDatabase().ExecSQL("DELETE FROM maps WHERE view_id=? AND sequence=?",
                                                               args);
                                if (RevisionInternal.CBLCompareRevIDs(oldRevId, revId) > 0)
                                {
                                    // It still 'wins' the conflict, so it's the one that
                                    // should be mapped [again], not the current revision!
                                    revId    = oldRevId;
                                    sequence = oldSequence;
                                    string[] selectArgs3 = new string[] { System.Convert.ToString(sequence) };
                                    json = Utils.ByteArrayResultForQuery(database.GetDatabase(), "SELECT json FROM revs WHERE sequence=?"
                                                                         , selectArgs3);
                                }
                            }
                        }
                        // Get the document properties, to pass to the map function:
                        EnumSet <Database.TDContentOptions> contentOptions = EnumSet.NoneOf <Database.TDContentOptions
                                                                                             >();
                        if (noAttachments)
                        {
                            contentOptions.AddItem(Database.TDContentOptions.TDNoAttachments);
                        }
                        IDictionary <string, object> properties = database.DocumentPropertiesFromJSON(json
                                                                                                      , docId, revId, false, sequence, contentOptions);
                        if (properties != null)
                        {
                            // Call the user-defined map() to emit new key/value
                            // pairs from this revision:
                            emitBlock.SetSequence(sequence);
                            mapBlock.Map(properties, emitBlock);
                        }
                    }
                }
                // Finally, record the last revision sequence number that was
                // indexed:
                ContentValues updateValues = new ContentValues();
                updateValues.Put("lastSequence", dbMaxSequence);
                string[] whereArgs_1 = new string[] { Sharpen.Extensions.ToString(GetViewId()) };
                database.GetDatabase().Update("views", updateValues, "view_id=?", whereArgs_1);
                // FIXME actually count number added :)
                Log.V(Log.TagView, "Finished re-indexing view: %s " + " up to sequence %s" + " (deleted %s added ?)"
                      , name, dbMaxSequence, deleted);
                result.SetCode(Status.Ok);
            }
            catch (SQLException e)
            {
                throw new CouchbaseLiteException(e, new Status(Status.DbError));
            }
            finally
            {
                if (cursor != null)
                {
                    cursor.Close();
                }
                if (!result.IsSuccessful())
                {
                    Log.W(Log.TagView, "Failed to rebuild view %s.  Result code: %d", name, result.GetCode
                              ());
                }
                if (database != null)
                {
                    database.EndTransaction(result.IsSuccessful());
                }
            }
        }
Esempio n. 16
0
            /// <exception cref="System.IO.IOException"/>
            public virtual int Run(Configuration conf, IList <string> args)
            {
                CacheDirectiveInfo.Builder builder = new CacheDirectiveInfo.Builder();
                bool   modified = false;
                string idString = StringUtils.PopOptionWithArgument("-id", args);

                if (idString == null)
                {
                    System.Console.Error.WriteLine("You must specify a directive ID with -id.");
                    return(1);
                }
                builder.SetId(long.Parse(idString));
                string path = StringUtils.PopOptionWithArgument("-path", args);

                if (path != null)
                {
                    builder.SetPath(new Path(path));
                    modified = true;
                }
                bool   force             = StringUtils.PopOption("-force", args);
                string replicationString = StringUtils.PopOptionWithArgument("-replication", args
                                                                             );

                if (replicationString != null)
                {
                    builder.SetReplication(short.ParseShort(replicationString));
                    modified = true;
                }
                string poolName = StringUtils.PopOptionWithArgument("-pool", args);

                if (poolName != null)
                {
                    builder.SetPool(poolName);
                    modified = true;
                }
                string ttlString = StringUtils.PopOptionWithArgument("-ttl", args);

                try
                {
                    CacheDirectiveInfo.Expiration ex = ParseExpirationString(ttlString);
                    if (ex != null)
                    {
                        builder.SetExpiration(ex);
                        modified = true;
                    }
                }
                catch (IOException e)
                {
                    System.Console.Error.WriteLine("Error while parsing ttl value: " + e.Message);
                    return(1);
                }
                if (!args.IsEmpty())
                {
                    System.Console.Error.WriteLine("Can't understand argument: " + args[0]);
                    System.Console.Error.WriteLine("Usage is " + GetShortUsage());
                    return(1);
                }
                if (!modified)
                {
                    System.Console.Error.WriteLine("No modifications were specified.");
                    return(1);
                }
                DistributedFileSystem dfs   = AdminHelper.GetDFS(conf);
                EnumSet <CacheFlag>   flags = EnumSet.NoneOf <CacheFlag>();

                if (force)
                {
                    flags.AddItem(CacheFlag.Force);
                }
                try
                {
                    dfs.ModifyCacheDirective(builder.Build(), flags);
                    System.Console.Out.WriteLine("Modified cache directive " + idString);
                }
                catch (IOException e)
                {
                    System.Console.Error.WriteLine(AdminHelper.PrettifyException(e));
                    return(2);
                }
                return(0);
            }
Esempio n. 17
0
 public void OnCompletion(object response, Exception e)
 {
     try
     {
         Log.V(Log.TagSync, "%s: got /_revs_diff response");
         IDictionary <string, object> results = (IDictionary <string, object>)response;
         if (e != null)
         {
             this._enclosing.SetError(e);
             this._enclosing.RevisionFailed();
         }
         else
         {
             if (results.Count != 0)
             {
                 // Go through the list of local changes again, selecting the ones the destination server
                 // said were missing and mapping them to a JSON dictionary in the form _bulk_docs wants:
                 IList <object> docsToSend = new AList <object>();
                 RevisionList   revsToSend = new RevisionList();
                 foreach (RevisionInternal rev in changes)
                 {
                     // Is this revision in the server's 'missing' list?
                     IDictionary <string, object> properties = null;
                     IDictionary <string, object> revResults = (IDictionary <string, object>)results.Get
                                                                   (rev.GetDocId());
                     if (revResults == null)
                     {
                         continue;
                     }
                     IList <string> revs = (IList <string>)revResults.Get("missing");
                     if (revs == null || !revs.Contains(rev.GetRevId()))
                     {
                         this._enclosing.RemovePending(rev);
                         continue;
                     }
                     // Get the revision's properties:
                     EnumSet <Database.TDContentOptions> contentOptions = EnumSet.Of(Database.TDContentOptions
                                                                                     .TDIncludeAttachments);
                     if (!this._enclosing.dontSendMultipart && this._enclosing.revisionBodyTransformationBlock
                         == null)
                     {
                         contentOptions.AddItem(Database.TDContentOptions.TDBigAttachmentsFollow);
                     }
                     RevisionInternal loadedRev;
                     try
                     {
                         loadedRev  = this._enclosing.db.LoadRevisionBody(rev, contentOptions);
                         properties = new Dictionary <string, object>(rev.GetProperties());
                     }
                     catch (CouchbaseLiteException)
                     {
                         Log.W(Log.TagSync, "%s Couldn't get local contents of %s", rev, this._enclosing);
                         this._enclosing.RevisionFailed();
                         continue;
                     }
                     RevisionInternal populatedRev      = this._enclosing.TransformRevision(loadedRev);
                     IList <string>   possibleAncestors = (IList <string>)revResults.Get("possible_ancestors"
                                                                                         );
                     properties = new Dictionary <string, object>(populatedRev.GetProperties());
                     IDictionary <string, object> revisions = this._enclosing.db.GetRevisionHistoryDictStartingFromAnyAncestor
                                                                  (populatedRev, possibleAncestors);
                     properties.Put("_revisions", revisions);
                     populatedRev.SetProperties(properties);
                     // Strip any attachments already known to the target db:
                     if (properties.ContainsKey("_attachments"))
                     {
                         // Look for the latest common ancestor and stub out older attachments:
                         int minRevPos = Couchbase.Lite.Replicator.Pusher.FindCommonAncestor(populatedRev,
                                                                                             possibleAncestors);
                         Database.StubOutAttachmentsInRevBeforeRevPos(populatedRev, minRevPos + 1, false);
                         properties = populatedRev.GetProperties();
                         if (!this._enclosing.dontSendMultipart && this._enclosing.UploadMultipartRevision
                                 (populatedRev))
                         {
                             continue;
                         }
                     }
                     if (properties == null || !properties.ContainsKey("_id"))
                     {
                         throw new InvalidOperationException("properties must contain a document _id");
                     }
                     revsToSend.AddItem(rev);
                     docsToSend.AddItem(properties);
                 }
                 //TODO: port this code from iOS
                 // Post the revisions to the destination:
                 this._enclosing.UploadBulkDocs(docsToSend, revsToSend);
             }
             else
             {
                 // None of the revisions are new to the remote
                 foreach (RevisionInternal revisionInternal in changes)
                 {
                     this._enclosing.RemovePending(revisionInternal);
                 }
             }
         }
     }
     finally
     {
         Log.V(Log.TagSync, "%s | %s: processInbox.sendAsyncRequest() calling asyncTaskFinished()"
               , this, Sharpen.Thread.CurrentThread());
         this._enclosing.AsyncTaskFinished(1);
     }
 }
Esempio n. 18
0
        /// <exception cref="System.Exception"/>
        public override int Run(string[] args)
        {
            Options opts  = new Options();
            string  title = null;

            if (args.Length > 0 && Sharpen.Runtime.EqualsIgnoreCase(args[0], Application))
            {
                title = Application;
                opts.AddOption(StatusCmd, true, "Prints the status of the application.");
                opts.AddOption(ListCmd, false, "List applications. " + "Supports optional use of -appTypes to filter applications "
                               + "based on application type, " + "and -appStates to filter applications based on application state."
                               );
                opts.AddOption(KillCmd, true, "Kills the application.");
                opts.AddOption(MoveToQueueCmd, true, "Moves the application to a " + "different queue."
                               );
                opts.AddOption(QueueCmd, true, "Works with the movetoqueue command to" + " specify which queue to move an application to."
                               );
                opts.AddOption(HelpCmd, false, "Displays help for all commands.");
                Option appTypeOpt = new Option(AppTypeCmd, true, "Works with -list to " + "filter applications based on "
                                               + "input comma-separated list of application types.");
                appTypeOpt.SetValueSeparator(',');
                appTypeOpt.SetArgs(Option.UnlimitedValues);
                appTypeOpt.SetArgName("Types");
                opts.AddOption(appTypeOpt);
                Option appStateOpt = new Option(AppStateCmd, true, "Works with -list " + "to filter applications based on input comma-separated list of "
                                                + "application states. " + GetAllValidApplicationStates());
                appStateOpt.SetValueSeparator(',');
                appStateOpt.SetArgs(Option.UnlimitedValues);
                appStateOpt.SetArgName("States");
                opts.AddOption(appStateOpt);
                opts.GetOption(KillCmd).SetArgName("Application ID");
                opts.GetOption(MoveToQueueCmd).SetArgName("Application ID");
                opts.GetOption(QueueCmd).SetArgName("Queue Name");
                opts.GetOption(StatusCmd).SetArgName("Application ID");
            }
            else
            {
                if (args.Length > 0 && Sharpen.Runtime.EqualsIgnoreCase(args[0], ApplicationAttempt
                                                                        ))
                {
                    title = ApplicationAttempt;
                    opts.AddOption(StatusCmd, true, "Prints the status of the application attempt.");
                    opts.AddOption(ListCmd, true, "List application attempts for aplication.");
                    opts.AddOption(HelpCmd, false, "Displays help for all commands.");
                    opts.GetOption(StatusCmd).SetArgName("Application Attempt ID");
                    opts.GetOption(ListCmd).SetArgName("Application ID");
                }
                else
                {
                    if (args.Length > 0 && Sharpen.Runtime.EqualsIgnoreCase(args[0], Container))
                    {
                        title = Container;
                        opts.AddOption(StatusCmd, true, "Prints the status of the container.");
                        opts.AddOption(ListCmd, true, "List containers for application attempt.");
                        opts.AddOption(HelpCmd, false, "Displays help for all commands.");
                        opts.GetOption(StatusCmd).SetArgName("Container ID");
                        opts.GetOption(ListCmd).SetArgName("Application Attempt ID");
                    }
                }
            }
            int         exitCode  = -1;
            CommandLine cliParser = null;

            try
            {
                cliParser = new GnuParser().Parse(opts, args);
            }
            catch (MissingArgumentException)
            {
                sysout.WriteLine("Missing argument for options");
                PrintUsage(title, opts);
                return(exitCode);
            }
            if (cliParser.HasOption(StatusCmd))
            {
                if (args.Length != 3)
                {
                    PrintUsage(title, opts);
                    return(exitCode);
                }
                if (Sharpen.Runtime.EqualsIgnoreCase(args[0], Application))
                {
                    exitCode = PrintApplicationReport(cliParser.GetOptionValue(StatusCmd));
                }
                else
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(args[0], ApplicationAttempt))
                    {
                        exitCode = PrintApplicationAttemptReport(cliParser.GetOptionValue(StatusCmd));
                    }
                    else
                    {
                        if (Sharpen.Runtime.EqualsIgnoreCase(args[0], Container))
                        {
                            exitCode = PrintContainerReport(cliParser.GetOptionValue(StatusCmd));
                        }
                    }
                }
                return(exitCode);
            }
            else
            {
                if (cliParser.HasOption(ListCmd))
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(args[0], Application))
                    {
                        allAppStates = false;
                        ICollection <string> appTypes = new HashSet <string>();
                        if (cliParser.HasOption(AppTypeCmd))
                        {
                            string[] types = cliParser.GetOptionValues(AppTypeCmd);
                            if (types != null)
                            {
                                foreach (string type in types)
                                {
                                    if (!type.Trim().IsEmpty())
                                    {
                                        appTypes.AddItem(StringUtils.ToUpperCase(type).Trim());
                                    }
                                }
                            }
                        }
                        EnumSet <YarnApplicationState> appStates = EnumSet.NoneOf <YarnApplicationState>();
                        if (cliParser.HasOption(AppStateCmd))
                        {
                            string[] states = cliParser.GetOptionValues(AppStateCmd);
                            if (states != null)
                            {
                                foreach (string state in states)
                                {
                                    if (!state.Trim().IsEmpty())
                                    {
                                        if (Sharpen.Runtime.EqualsIgnoreCase(state.Trim(), AllstatesOption))
                                        {
                                            allAppStates = true;
                                            break;
                                        }
                                        try
                                        {
                                            appStates.AddItem(YarnApplicationState.ValueOf(StringUtils.ToUpperCase(state).Trim
                                                                                               ()));
                                        }
                                        catch (ArgumentException)
                                        {
                                            sysout.WriteLine("The application state " + state + " is invalid.");
                                            sysout.WriteLine(GetAllValidApplicationStates());
                                            return(exitCode);
                                        }
                                    }
                                }
                            }
                        }
                        ListApplications(appTypes, appStates);
                    }
                    else
                    {
                        if (Sharpen.Runtime.EqualsIgnoreCase(args[0], ApplicationAttempt))
                        {
                            if (args.Length != 3)
                            {
                                PrintUsage(title, opts);
                                return(exitCode);
                            }
                            ListApplicationAttempts(cliParser.GetOptionValue(ListCmd));
                        }
                        else
                        {
                            if (Sharpen.Runtime.EqualsIgnoreCase(args[0], Container))
                            {
                                if (args.Length != 3)
                                {
                                    PrintUsage(title, opts);
                                    return(exitCode);
                                }
                                ListContainers(cliParser.GetOptionValue(ListCmd));
                            }
                        }
                    }
                }
                else
                {
                    if (cliParser.HasOption(KillCmd))
                    {
                        if (args.Length != 3)
                        {
                            PrintUsage(title, opts);
                            return(exitCode);
                        }
                        try
                        {
                            KillApplication(cliParser.GetOptionValue(KillCmd));
                        }
                        catch (ApplicationNotFoundException)
                        {
                            return(exitCode);
                        }
                    }
                    else
                    {
                        if (cliParser.HasOption(MoveToQueueCmd))
                        {
                            if (!cliParser.HasOption(QueueCmd))
                            {
                                PrintUsage(title, opts);
                                return(exitCode);
                            }
                            MoveApplicationAcrossQueues(cliParser.GetOptionValue(MoveToQueueCmd), cliParser.GetOptionValue
                                                            (QueueCmd));
                        }
                        else
                        {
                            if (cliParser.HasOption(HelpCmd))
                            {
                                PrintUsage(title, opts);
                                return(0);
                            }
                            else
                            {
                                syserr.WriteLine("Invalid Command Usage : ");
                                PrintUsage(title, opts);
                            }
                        }
                    }
                }
            }
            return(0);
        }