Bug 1576408 - Monitoring tab API error tags category.categorization [NEEDINFO]
Summary: Monitoring tab API error tags category.categorization
Keywords:
Status: CLOSED INSUFFICIENT_DATA
Alias: None
Product: Red Hat CloudForms Management Engine
Classification: Red Hat
Component: API
Version: 5.9.0
Hardware: Unspecified
OS: Unspecified
medium
high
Target Milestone: GA
: cfme-future
Assignee: Joe Rafaniello
QA Contact: Martin Kourim
URL:
Whiteboard:
Depends On:
Blocks: 1595269
TreeView+ depends on / blocked
 
Reported: 2018-05-09 11:51 UTC by Gellert Kis
Modified: 2019-11-06 08:49 UTC (History)
7 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
5.9.1
Last Closed: 2018-07-25 21:05:17 UTC
Category: ---
Cloudforms Team: CFME Core
Target Upstream Version:
jrafanie: needinfo? (gekis)


Attachments (Terms of Use)

Comment 2 Joe Rafaniello 2018-05-09 21:44:43 UTC
Hi Gellert, from the information provided, it appears that we have either a tag 
1) with an orphaned classification
2) with an orphaned category 

https://github.com/ManageIQ/manageiq/blob/9c36ec80081c490dd110bcb019b7149052487620/app/models/tag.rb#L154-L156

We might be able to do something in rails console to loop through all the tags and find the problematic ones or you could dump the tags and classifications tables and provide those.  I'm not exactly sure how this happened.  Was there a migration done recently?  Were tags exported/imported into this system?  A dump of those tables would be most helpful.

Thanks!

Comment 4 Joe Rafaniello 2018-05-10 16:22:36 UTC
Something like this should get you the data from the tags and classifications tables:

pg_dump -a -U root -d vmdb_production -t tags -t classifications > /tmp/tag_classifications.sql

Comment 6 Joe Rafaniello 2018-05-11 14:26:55 UTC
Hi Gellert,

You can do this:

1) copy and paste this code into a file in /var/www/miq/vmdb/tools/find_orphaned_tags.rb (it's important that it's in the tools directory)


#!/usr/bin/env ruby
require File.expand_path("../config/environment", __dir__)

Tag.find_each do |t|
  classif = t.classification
  cat     = t.category
  if classif.nil? || cat.nil?
    msg = t.id.to_s.ljust(20)
    msg << t.name.ljust(50)
    msg << "no classif: #{classif.nil?.to_s.ljust(8)}"
    msg << "no category: #{cat.nil?}"
    puts msg
  end
end

2) vmdb # to CD into /var/www/miq/vmdb
3) ruby tools/find_orphaned_tags.rb > orphans.log
4) Review /var/www/miq/vmdb/orphans.log and please attach it to this bug as a private attachment.


I have a few orphan tags in some of my local databases so I'll look into what I have already and see if I can recreate it, although this could be normal.  I need to see if there are specific tags that are tripping us up in the API request you posted for this BZ.

Thanks!

Comment 7 Joe Rafaniello 2018-05-11 15:02:23 UTC
Hi Gellert, I was able to recreate by hacking my database in rails console.

DO NOT DO THIS ON YOUR DATABASE.

Basically, they have a tag with a category but an orphaned classification.

1) Before (in rails console):

irb(main):001:0> Tag.all.select {|t| t.category && t.classification.nil? }
=> []

irb(main):013:0> t = Tag.find(127)
=> #<Tag id: 127, ..."

irb(main):014:0> t.category
=> #<Classification id: 127, ...>

irb(main):015:0> t.classification
=> #<Classification id: 125, ...>

2) Now, I manually remove the tag's classification without removing the category:

irb(main):016:0> t = Tag.find(127)

irb(main):024:0> t.classification.delete
=> #<Classification id: 125, ...>

We now check for tags with a category but not a classification:

irb(main):026:0> Tag.all.select {|t| t.category && t.classification.nil? }
=> [#<Tag id: 127, ...>]


3) I now get this when I hit my appliance URL:
https://x.x.x.x/api/tags?expand=resources&attributes=categorization

{"error":{"kind":"internal_server_error","message":"undefined method `name' for nil:NilClass","klass":"NoMethodError"}}

Comment 8 Joe Rafaniello 2018-05-11 15:29:44 UTC
Here is an updated version of the script that will print just the Tags with a category but not a classification... ones that will cause the API request to blow up.

Note, we won't know the correct way to fix this until we know how many there are and what they are.  The only recourse seems to be to remove the tags and recreate them if they're still needed.  Based on what they are, we will have to try to determine how they became orphaned.

#!/usr/bin/env ruby
require File.expand_path("../config/environment", __dir__)

Tag.find_each do |t|
  classif = t.classification
  cat     = t.category
  # find tags with a category but no classification
  # Maybe something did tag.classification.delete instead of destroy???
  if cat && classif.nil?
    msg = t.id.to_s.ljust(20)
    msg << t.name.ljust(50)
    msg << "no classif: #{classif.nil?.to_s.ljust(8)}"
    msg << "category: #{cat.id.to_s.ljust(20)}"
    msg << cat.name
    puts msg
  end
end

Comment 9 Joe Rafaniello 2018-05-11 15:31:07 UTC
Please run the script in comment 8

1) copy and paste the code in comment 8 into a file in /var/www/miq/vmdb/tools/find_orphaned_tags.rb (it's important that it's in the tools directory)

2) vmdb # to CD into /var/www/miq/vmdb
3) ruby tools/find_orphaned_tags.rb > orphans.log
4) Review /var/www/miq/vmdb/orphans.log and please attach it to this bug as a private attachment.

Comment 10 Joe Rafaniello 2018-05-11 16:24:06 UTC
Possibly related upstream (unmerged) PR/commit:

https://github.com/ManageIQ/manageiq/pull/17210/commits/a5a7466363e69e4b267f0575c1c5ef45e8517f4c

Comment 14 Joe Rafaniello 2018-05-16 14:32:08 UTC
Niladri, are the prior comments for this customer's reported problem or some other case you're looking at?

> Cu runs a script which creates new tag in existing category.

> The problem then isn't with creating/deleting tags, they *are* there but with listing the resources/tags.

If the scripts are creating orphaned tags (tags without a classification, but with a category classification), then it is definitely a problem with incorrectly creating the tags via the script.

>I also got the the output of the ruby script from comment 8

Are these /managed/* tags in comment 11 (the output from comment 8 script) created by their scripts?  If so, there's a problem, because they're not creating the tag properly.

These tags need to have a classification entry and a classification category but only have a category.  This is why the API categorization is failing.

These tags need to be either be fixed or recreated properly.

Comment 19 Joe Rafaniello 2018-05-17 13:37:22 UTC
Hi Niladri,

If they are hitting the undefined method name for nil:NilClass error doing the  &attributes=category,categorization and they have the output you showed in comment 11, then they have some managed tags that are orphaned.  These tags need to be either fixed or removed before you can troubleshoot anything else.

If a tag has a category (which is just a classification with parent_id 0), it needs to have a classification (parent_id non-zero).  Note, it is possible to have a tag with neither a category or classification, but for this problem, if you have a category, you must have a classification.

For example, this is how it's correctly hooked up for a tag with a category:

The tag has a classification, who's parent is the tag's category:
tag.classification.parent_id == tag.category.id  # 

The category has no parent:
tag.category.parent_id == 0

In rails console:

irb(main):020:0> t = Tag.last
=> #<Tag id: 143, name: "/managed/v2v_transformation_method/ssh">
irb(main):021:0> t.category.id
=> 139
irb(main):022:0> t.category.parent_id
=> 0
irb(main):024:0> t.classification.parent_id
=> 139  # classification id of the category

In the output provided in comment 11, we are missing  the tag.classification, while the tag has a category.  This must be fixed or the tag must be removed.

I'm not sure how they were created but something failed to create them properly.

Comment 21 Joe Rafaniello 2018-05-18 17:23:17 UTC
Niladri,

That script seems ok.  It's not the whole script so I can't tell for sure, but from testing in the manageiq-api repo, the "create a tag with a category by id" seems to create the tag with a classification and a category:

https://github.com/ManageIQ/manageiq-api/blob/e30003a9baecf1e559cc25fd8c4b4121cde1c2f8/spec/requests/tags_spec.rb#L44

Please test the script to see if it causes the script in comment 8 to find "new" orphaned tags after running the script.

If the script is incorrectly creating tags, please have them open a new BZ with the full script, what happens and the output from comment 8 script.

At this point, I don't even know if this specific customer issue is related to the original problem reported by Gellert.  I'd rather not complicate this BZ by handling multiple problems in it.

Comment 25 Joe Rafaniello 2018-06-28 15:56:18 UTC
Hi Gellert,

Do you have an update on this issue?

Thanks,
Joe

Comment 28 Joe Rafaniello 2018-07-25 21:05:17 UTC
Gellert, I'm closing this issue.  If it's still an issue, please reopen and provide the requested information.  Thank you!


Note You need to log in before you can comment on or make changes to this bug.