DT icon indicating copy to clipboard operation
DT copied to clipboard

Ajax Error with Load Balancer on Chrome

Open jdonnell01 opened this issue 7 years ago • 27 comments

I recently implemented a load balancer for my R Shiny application with two separate instances of the application running. On occasion when I navigate to the load balancer URL I see the following JS alert:

DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7

In the console I see a 404 Not Found error on all my datatable objects. I am using S3 and grabbing all of my .rds files when the application loads. All other objects load as expected except those that will be rendered as a datatable. This only seems to occur on Chrome and only happens about 25% of the time - the error seems quite erratic. Is this a known issue or has anyone seen this happen before?

jdonnell01 avatar Mar 07 '19 14:03 jdonnell01

A simple workaround for your case is to use client mode for all your DT tables (if your table is not too big to use client mode).

As for the cause, it seems like the AJAX request is forwarded to the wrong worker, which I suppose is tied to your load balancer implementations.

https://stackoverflow.com/questions/26242541/ajax-request-and-load-balance this may help.

shrektan avatar Mar 08 '19 06:03 shrektan

This is called sticky session or persistent session in load rebalancing.

shrektan avatar Mar 08 '19 18:03 shrektan

Thank you - to confirm, by client mode you are referring to the below? datatable(mtcars, rownames = FALSE, options = list(ajax = list(serverSide = FALSE)))

I tested the above and the issue still occurs around 10% of the time.

jdonnell01 avatar Mar 08 '19 18:03 jdonnell01

No, I mean DT::renderDT(..., server=False). See ?DT::renderDT

server : whether to use server-side processing. If TRUE, then the data is kept on the server and the browser requests a page at a time; if FALSE, then the entire data frame is sent to the browser at once. Highly recommended for medium to large data frames, which can cause browsers to slow down or crash.

shrektan avatar Mar 09 '19 00:03 shrektan

Thank you. Disabling server-side processing seems to be affecting the input$dtname_rows_selected. With server = TRUE, I am able to output the index of the row selected, yet with server=FALSE the output of input$dtname_rows_selected is NULL.

jdonnell01 avatar Mar 12 '19 22:03 jdonnell01

No, it should not. Please test it on local. If there’s still a problem, a reproducible example is welcomed. Thanks.

shrektan avatar Mar 12 '19 23:03 shrektan

I see this problem too . It occurs a few seconds after I open the page . When I checkout the problem, it shows the post request return a 404 not found error. DT version : 0.5

lengyuyeke avatar Mar 13 '19 03:03 lengyuyeke

Hi @shrektan. After further investigation I see that the rows_selected functionality is actually operating correctly, but rather I instantiated an action button that when clicked will select the appropriate row in the datatable which is now failing went I set server = FALSE:

observe({
  lapply (1:length(unique(my_dt$FIELD1)), function (i) {
    observeEvent(input[["button"]], {
      proxy = dataTableProxy('TABLE1')
      proxy %>% selectRows(as.numeric(i))
    })
  })
})

jdonnell01 avatar Mar 13 '19 22:03 jdonnell01

The following code works for me. If you still have problems, please provide a minimal reproducible example so that we can help. Thanks.

library(shiny)

ui <- fluidPage(
  actionButton('select', 'select'),
  DT::DTOutput('tbl')
)
server <- function(input, output, session) {
  output$tbl <- DT::renderDT(iris, server = FALSE)
  proxy <- DT::dataTableProxy('tbl')
  observeEvent(input$select, {
    DT::selectRows(proxy, sample(1:10, 1))
  })
}

shiny::runApp(mget(c('ui', 'server')))

shrektan avatar Mar 14 '19 05:03 shrektan

Hello @shrektan. Your example does not seem to be working for me. I included a text output to your example to print the current selected row and when I set server=FALSE there is no text output, however when server=TRUE it seems to select a random row.

Does not select a random row on button click

library(shiny)

ui <- fluidPage(
  actionButton('select', 'select'),
  DT::DTOutput('tbl'),
  htmlOutput('selectedrow')
)
server <- function(input, output, session) {
  output$tbl <- DT::renderDT(iris, server = FALSE)
  proxy <- DT::dataTableProxy('tbl')
  observeEvent(input$select, {
    DT::selectRows(proxy, sample(1:10, 1))
  })
  output$selectedrow <- renderText(input$tbl_rows_selected)
}

shiny::runApp(mget(c('ui', 'server')))

Selects a random row on button click

library(shiny)

ui <- fluidPage(
  actionButton('select', 'select'),
  DT::DTOutput('tbl'),
  htmlOutput('selectedrow')
)
server <- function(input, output, session) {
  output$tbl <- DT::renderDT(iris, server = TRUE)
  proxy <- DT::dataTableProxy('tbl')
  observeEvent(input$select, {
    DT::selectRows(proxy, sample(1:10, 1))
  })
  output$selectedrow <- renderText(input$tbl_rows_selected)
}

shiny::runApp(mget(c('ui', 'server')))

jdonnell01 avatar Mar 26 '19 19:03 jdonnell01

Hi @shrektan. Just following up here - would you categorize this as a bug?

jdonnell01 avatar Apr 01 '19 13:04 jdonnell01

No, both versions work on my computer. Are you sure you are using the latest version of DT? If not, please install and re-test it by running remotes::install_github('Rstudio/DT').

If still had problems, please paste your sessionInfo() here. Thanks.

shrektan avatar Apr 01 '19 14:04 shrektan

Thank you - I will test with the latest version. Are you able to update CRAN with the latest version?

jdonnell01 avatar Apr 01 '19 15:04 jdonnell01

We can update DT on CRAN this month if it would fix your issue.

yihui avatar Apr 01 '19 15:04 yihui

Thank you - this seems to be working now. One final question - now that I have flipped my tables to client-side processing and try to filter on columns it is extremely slow (~15 - 20 seconds to respond). Is there any way to mitigate this slowless?

jdonnell01 avatar Apr 01 '19 22:04 jdonnell01

I’m afraid not.

Anyway, generally it should be fast enough unless your table contains dozens of columns and you are using IE...

shrektan avatar Apr 01 '19 23:04 shrektan

The table contains 8 columns, however I am using IE. Are there any steps to remediate for IE use?

jdonnell01 avatar Apr 01 '19 23:04 jdonnell01

So your table contains tons of characters or thousands of rows? For small datasets, it should be fast. However, on some old computers, using IE is just 10 times slower.

I can only suggest you:

  • Update your IE version (not sure if related but you can try to disable the compatible view mode),
  • Use server processing mode,
  • Or, use Chrome.

shrektan avatar Apr 02 '19 03:04 shrektan

There are actually only 10 columns and roughly 700 rows. Understood - if there is any way to mitigate the ajax error without switching to client-side processing I will do that. Is that possible?

jdonnell01 avatar Apr 02 '19 14:04 jdonnell01

IMHO, you need to configure the sticky sessions for your load balancer. If you are using Nginx, adding ip_hash to the directive should be enough (I never test this but should be easy to google)...

shrektan avatar Apr 02 '19 15:04 shrektan