Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[CHEF-2927] Warn if the maxFieldLength is too low during chef-solr st…

…artup
  • Loading branch information...
commit 8b1f770126934e29a5d264e4282e3be9060374c8 1 parent 719f4d2
Tommy Bishop authored February 13, 2012 aglarond committed April 11, 2012
39  chef-solr/lib/chef/solr/application/solr.rb
@@ -122,6 +122,9 @@ def schema_file_path
122 122
           @schema_file_path ||= File.join(Chef::Config[:solr_home_path], 'conf', 'schema.xml')
123 123
         end
124 124
 
  125
+        def solr_config_file_path
  126
+          @solr_config_file_path ||= File.join(Chef::Config[:solr_home_path], 'conf', 'solrconfig.xml')
  127
+        end
125 128
 
126 129
         def schema_document
127 130
           @schema_document ||= begin
@@ -131,10 +134,23 @@ def schema_document
131 134
           end
132 135
         end
133 136
 
  137
+        def config_document
  138
+          @config_document ||=begin
  139
+            File.open(solr_config_file_path, 'r') do |xmlsux|
  140
+              REXML::Document.new(xmlsux)
  141
+            end
  142
+          end
  143
+        end
  144
+
134 145
         def schema_attributes
135 146
           @schema_attributes ||= REXML::XPath.first(schema_document, '/schema').attributes
136 147
         end
137 148
 
  149
+        def solr_main_index_elements
  150
+          location = '/config/mainIndex/'
  151
+          @solr_main_index_elements ||= REXML::XPath.first(config_document, location).elements
  152
+        end
  153
+
138 154
         def solr_schema_name
139 155
           schema_attributes["name"]
140 156
         end
@@ -143,6 +159,16 @@ def solr_schema_version
143 159
           schema_attributes["version"]
144 160
         end
145 161
 
  162
+        def solr_main_index_max_field_length
  163
+          @solr_main_index_max_field_length ||=begin
  164
+            field_length_el = solr_main_index_elements.select do |el|
  165
+              el.name == 'maxFieldLength'
  166
+            end
  167
+
  168
+            field_length_el.empty? ? nil : field_length_el.first.text.to_i
  169
+          end
  170
+        end
  171
+
146 172
         def valid_schema_name?
147 173
           solr_schema_name == Chef::Solr::SCHEMA_NAME
148 174
         end
@@ -151,6 +177,18 @@ def valid_schema_version?
151 177
           solr_schema_version == Chef::Solr::SCHEMA_VERSION
152 178
         end
153 179
 
  180
+        def check_value_of_main_index_max_field_length
  181
+          if solr_main_index_max_field_length
  182
+            unless solr_main_index_max_field_length > 10000
  183
+              message  = "The maxFieldLimit for the mainIndex is set to #{solr_main_index_max_field_length}.  "
  184
+              message << "It's recommended to increase this value (in #{solr_config_file_path})."
  185
+              Chef::Log.warn message
  186
+            end
  187
+          else
  188
+            Chef::Log.warn "Unable to determine the maxFieldLimit for the mainIndex (in #{solr_config_file_path})"
  189
+          end
  190
+        end
  191
+
154 192
         def solr_home_exist?
155 193
           File.directory?(Chef::Config[:solr_home_path])
156 194
         end
@@ -187,6 +225,7 @@ def assert_valid_schema!
187 225
         def setup_application
188 226
           assert_solr_installed!
189 227
           assert_valid_schema!
  228
+          check_value_of_main_index_max_field_length
190 229
 
191 230
           # Need to redirect stdout and stderr so Java process inherits them.
192 231
           # If -L wasn't specified, Chef::Config[:log_location] will be an IO
131  chef-solr/spec/unit/application/solr_spec.rb
@@ -42,23 +42,58 @@
42 42
 
43 43
   end
44 44
 
  45
+  describe 'solr_config_file_path' do
  46
+    it 'should return the default solr config path' do
  47
+      subject.solr_config_file_path.should == '/var/chef/solr/conf/solrconfig.xml'
  48
+    end
  49
+
  50
+    context 'with a custom solr home path' do
  51
+      it 'should return the solr config path' do
  52
+        Chef::Config.stub(:[]).with(:solr_home_path).
  53
+                               and_return('/opt/chef/solr')
  54
+        subject.solr_config_file_path.should == '/opt/chef/solr/conf/solrconfig.xml'
  55
+      end
  56
+    end
  57
+
  58
+  end
  59
+
45 60
   describe 'schema_document' do
46 61
     before do
47 62
       @schema_path = '/opt/chef/solr/conf/schema.xml'
48  
-      @file = stub
49 63
       subject.stub :schema_file_path => @schema_path
  64
+      @doc_contents = '<?xml version="1.0" encoding="UTF-8" ?><foo>bar</foo>'
50 65
     end
51 66
 
52 67
     it 'should read the schema file at the correct path' do
53 68
       REXML::Document.stub(:new)
54  
-      File.should_receive(:open).with(@schema_path, 'r').and_yield(@file)
  69
+      File.should_receive(:open).with(@schema_path, 'r').
  70
+                                 and_yield(@doc_contents)
55 71
       subject.schema_document
56 72
     end
57 73
 
58 74
     it 'should return the schema' do
59  
-      File.stub(:open).and_yield(@file)
60  
-      REXML::Document.should_receive(:new).and_return('foo bar schema')
61  
-      subject.schema_document.should == 'foo bar schema'
  75
+      File.stub(:open).and_yield(@doc_contents)
  76
+      subject.schema_document.should be_a REXML::Document
  77
+    end
  78
+  end
  79
+
  80
+  describe 'config_document' do
  81
+    before do
  82
+      @solr_config_path = '/opt/chef/solr/conf/solrconfig.xml'
  83
+      subject.stub :solr_config_file_path => @solr_config_path
  84
+      @doc_contents = '<?xml version="1.0" encoding="UTF-8" ?><foo>bar</foo>'
  85
+    end
  86
+
  87
+    it 'should read the config file at the correct path' do
  88
+      REXML::Document.stub(:new)
  89
+      File.should_receive(:open).with(@solr_config_path, 'r').
  90
+                                 and_yield(@doc_contents)
  91
+      subject.config_document
  92
+    end
  93
+
  94
+    it 'should return an REXML document' do
  95
+      File.stub(:open).and_yield(@doc_contents)
  96
+      subject.config_document.should be_a REXML::Document
62 97
     end
63 98
   end
64 99
 
@@ -74,6 +109,26 @@
74 109
     end
75 110
   end
76 111
 
  112
+  describe 'solr_main_index_elements' do
  113
+    before do
  114
+      doc_contents = '<?xml version="1.0" encoding="UTF-8" ?>'
  115
+      doc_contents << '<config><mainIndex>'
  116
+      doc_contents << '<maxFieldLength>10000</maxFieldLength>'
  117
+      doc_contents << '</mainIndex></config>'
  118
+      subject.stub(:config_document).
  119
+              and_return(REXML::Document.new(doc_contents))
  120
+    end
  121
+
  122
+    it 'should return a collection of the REXML elements' do
  123
+      subject.solr_main_index_elements.each { |e| e.should be_a REXML::Element }
  124
+    end
  125
+
  126
+    it 'should return the correct elements' do
  127
+      subject.solr_main_index_elements.first.name.should == 'maxFieldLength'
  128
+      subject.solr_main_index_elements.first.text.should == '10000'
  129
+    end
  130
+  end
  131
+
77 132
   describe 'solr_schema_name' do
78 133
     it 'should return the schema name' do
79 134
       subject.stub :schema_attributes => { 'name' => 'chef' }
@@ -88,13 +143,38 @@
88 143
     end
89 144
   end
90 145
 
  146
+  describe 'solr_main_index_max_field_length' do
  147
+    before do
  148
+      @elements = [ REXML::Element.new('useCompoundFile').add_text('false'),
  149
+                    REXML::Element.new('ramBufferSizeMB').add_text('32'),
  150
+                    REXML::Element.new('maxFieldLength').add_text('10000') ]
  151
+      subject.stub :solr_main_index_elements => @elements
  152
+    end
  153
+
  154
+    it 'should return the value of maxFieldLimit as an integer' do
  155
+      subject.solr_main_index_max_field_length.should == 10000
  156
+    end
  157
+
  158
+    context 'if unable to find the maxFieldLimit' do
  159
+      before do
  160
+        elements = @elements.select { |e| e.name != 'maxFieldLength' }
  161
+        subject.stub :solr_main_index_elements => elements
  162
+      end
  163
+
  164
+      it 'should return nil' do
  165
+        subject.solr_main_index_max_field_length.should be_nil
  166
+      end
  167
+    end
  168
+
  169
+  end
  170
+
91 171
   describe 'valid_schema_name?' do
92 172
     it 'should return true if the schema name matches' do
93 173
       subject.stub :solr_schema_name => Chef::Solr::SCHEMA_NAME
94 174
       subject.valid_schema_name?.should be_true
95 175
     end
96 176
 
97  
-    it 'should return false if the schema name does not matche' do
  177
+    it 'should return false if the schema name does not match' do
98 178
       subject.stub :solr_schema_name => 'foo'
99 179
       subject.valid_schema_name?.should be_false
100 180
     end
@@ -106,12 +186,37 @@
106 186
       subject.valid_schema_version?.should be_true
107 187
     end
108 188
 
109  
-    it 'should return false if the version name does not matche' do
  189
+    it 'should return false if the version name does not match' do
110 190
       subject.stub :solr_schema_version => '-1.0'
111 191
       subject.valid_schema_version?.should be_false
112 192
     end
113 193
   end
114 194
 
  195
+  describe 'check_value_of_main_index_max_field_length' do
  196
+    it 'should log a warning if it is set to <= 10000' do
  197
+      subject.stub :solr_main_index_max_field_length => 10000
  198
+      pattern = /maxFieldLimit.+set to.+recommended to increase this value/
  199
+      Chef::Log.should_receive(:warn).with(pattern)
  200
+      subject.check_value_of_main_index_max_field_length
  201
+    end
  202
+
  203
+    it 'should not log a warning if it is set to > 10000' do
  204
+      subject.stub :solr_main_index_max_field_length => 10001
  205
+      Chef::Log.should_not_receive(:warn)
  206
+      subject.check_value_of_main_index_max_field_length
  207
+    end
  208
+
  209
+    context 'if it is not set' do
  210
+      it 'should log a warning if it is not set' do
  211
+        subject.stub :solr_main_index_max_field_length => nil
  212
+        Chef::Log.should_receive(:warn).
  213
+                  with(/Unable to determine the maxFieldLimit for the mainIndex/)
  214
+        subject.check_value_of_main_index_max_field_length
  215
+      end
  216
+    end
  217
+
  218
+  end
  219
+
115 220
   describe 'solr_home_exists?' do
116 221
     before do
117 222
       Chef::Config.stub(:[]).with(:solr_home_path).
@@ -273,7 +378,7 @@
273 378
 
274 379
     end
275 380
 
276  
-    context 'when schema name and version are valid' do
  381
+    context 'when the schema name and version are valid' do
277 382
       before do
278 383
         ['name', 'version'].each do |item|
279 384
           subject.stub "valid_schema_#{item}?".to_sym => true
@@ -294,20 +399,30 @@
294 399
 
295 400
     it 'should see if solr is installed' do
296 401
       subject.stub :assert_valid_schema!
  402
+      subject.stub :check_value_of_main_index_max_field_length
297 403
       subject.should_receive :assert_solr_installed!
298 404
       subject.setup_application
299 405
     end
300 406
 
301 407
     it 'should see if the schema is valid' do
302 408
       subject.stub :assert_solr_installed!
  409
+      subject.stub :check_value_of_main_index_max_field_length
303 410
       subject.should_receive :assert_valid_schema!
304 411
       subject.setup_application
305 412
     end
306 413
 
  414
+    it 'should check the maxFieldLimit setting' do
  415
+      subject.stub :assert_solr_installed!
  416
+      subject.stub :assert_valid_schema!
  417
+      subject.should_receive :check_value_of_main_index_max_field_length
  418
+      subject.setup_application
  419
+    end
  420
+
307 421
     context 'with solr installed and a valid schema' do
308 422
       before do
309 423
         subject.stub :assert_solr_installed!
310 424
         subject.stub :assert_valid_schema!
  425
+        subject.stub :check_value_of_main_index_max_field_length
311 426
       end
312 427
 
313 428
       context 'with -L or --logfile' do

0 notes on commit 8b1f770

Please sign in to comment.
Something went wrong with that request. Please try again.